UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.

93 lines (92 loc) 4.07 kB
import { INTERNALS } from '../constants.js'; /** * Plugin to inject `relative time` related methods */ export const relativeTimePlugin = (ChronosClass) => { ChronosClass.prototype.getRelativeYear = function (time) { const inputDate = ChronosClass[INTERNALS].internalDate(this); const now = ChronosClass[INTERNALS].toNewDate(this, time); let years = inputDate.getFullYear() - now.getFullYear(); const noYearMonthDay = now.getMonth() < inputDate.getMonth() || (now.getMonth() === inputDate.getMonth() && now.getDate() < inputDate.getDate()); if (noYearMonthDay) { years--; } return years; }; ChronosClass.prototype.getRelativeMonth = function (time) { const now = ChronosClass[INTERNALS].toNewDate(this, time); const inputDate = ChronosClass[INTERNALS].internalDate(this); let months = (inputDate.getFullYear() - now.getFullYear()) * 12 + (inputDate.getMonth() - now.getMonth()); const hasNotHadMonthDay = now.getDate() < inputDate.getDate(); if (hasNotHadMonthDay) { months--; } return months; }; ChronosClass.prototype.getRelativeWeek = function (time) { const relativeDays = this.getRelativeDay(time); return Math.floor(relativeDays / 7); }; ChronosClass.prototype.getRelativeDay = function (time) { const today = ChronosClass[INTERNALS].toNewDate(this, time); // Set the time of today to 00:00:00 for comparison purposes today.setHours(0, 0, 0, 0); // Normalize the input date to 00:00:00 const inputDate = ChronosClass[INTERNALS].internalDate(this); inputDate.setHours(0, 0, 0, 0); const diffTime = inputDate.getTime() - today.getTime(); const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24)); return diffDays; }; ChronosClass.prototype.getRelativeHour = function (time) { const diff = ChronosClass[INTERNALS].internalDate(this).getTime() - ChronosClass[INTERNALS].toNewDate(this, time).getTime(); return Math.floor(diff / (1000 * 60 * 60)); }; ChronosClass.prototype.getRelativeMinute = function (time) { const diff = ChronosClass[INTERNALS].internalDate(this).getTime() - ChronosClass[INTERNALS].toNewDate(this, time).getTime(); return Math.floor(diff / (1000 * 60)); }; ChronosClass.prototype.getRelativeSecond = function (time) { const diff = ChronosClass[INTERNALS].internalDate(this).getTime() - ChronosClass[INTERNALS].toNewDate(this, time).getTime(); return Math.floor(diff / 1000); }; ChronosClass.prototype.getRelativeMilliSecond = function (time) { return (ChronosClass[INTERNALS].internalDate(this).getTime() - ChronosClass[INTERNALS].toNewDate(this, time).getTime()); }; ChronosClass.prototype.compare = function (unit = 'minute', time) { switch (unit) { case 'year': return this.getRelativeYear(time); case 'month': return this.getRelativeMonth(time); case 'day': return this.getRelativeDay(time); case 'week': return this.getRelativeWeek(time); case 'hour': return this.getRelativeHour(time); case 'minute': return this.getRelativeMinute(time); case 'second': return this.getRelativeSecond(time); case 'millisecond': return this.getRelativeMilliSecond(time); default: throw new Error(`Unsupported time unit: ${unit}`); } }; ChronosClass.prototype.isToday = function () { return this.getRelativeDay() === 0; }; ChronosClass.prototype.isTomorrow = function () { return this.getRelativeDay() === 1; }; ChronosClass.prototype.isYesterday = function () { return this.getRelativeDay() === -1; }; };