UNPKG

@dvcol/common-utils

Version:

Typescript library for common utility functions and constants

118 lines (116 loc) 3.69 kB
// lib/common/utils/date.utils.ts var elapsedTime = (endDate, startDate = /* @__PURE__ */ new Date()) => { return (endDate.getTime() - startDate.getTime()) / 1e3; }; var DateUtils = class { static clone = (date) => new Date(date); static next(days = 1, date, coefficient = 1) { const _date = date ? this.clone(date) : /* @__PURE__ */ new Date(); _date.setDate(_date.getDate() + days * coefficient); return _date; } static previous(days = 1, date, coefficient = 1) { return this.next(days, date, -coefficient); } static weeks = { previous: (weeks = 1, date) => this.previous(weeks, date, 7), next: (weeks = 1, date) => this.next(weeks, date, 7) }; }; var toDateObject = (record) => { if (!record) return void 0; return Object.fromEntries( Object.entries(record).map(([key, value]) => { if (typeof value === "string") return [key, new Date(value)]; if (typeof value === "object") return [key, toDateObject(value)]; return [key, value]; }) ); }; var isDate = (value) => value instanceof Date; var compareDateObject = (a, b) => { if (!a || !b) throw new Error("Cannot compare undefined objects"); return Object.fromEntries( Object.keys(a).map((key) => { const _key = key; if (a && b) { const aValue = a[_key]; const bValue = b[_key]; if (isDate(aValue) && isDate(bValue)) return [_key, aValue.getTime() !== bValue.getTime()]; if (!isDate(aValue) && !isDate(bValue)) return [_key, compareDateObject(aValue, bValue)]; } return [_key, a !== b]; }) ); }; var shortTime = (date, locale, options) => date?.toLocaleTimeString(locale, { hour: "2-digit", minute: "2-digit", ...options }); var DayOfWeek = { Sunday: "Sunday", Monday: "Monday", Tuesday: "Tuesday", Wednesday: "Wednesday", Thursday: "Thursday", Friday: "Friday", Saturday: "Saturday" }; var DayOfWeekToNumber = { [DayOfWeek.Sunday]: 0, [DayOfWeek.Monday]: 1, [DayOfWeek.Tuesday]: 2, [DayOfWeek.Wednesday]: 3, [DayOfWeek.Thursday]: 4, [DayOfWeek.Friday]: 5, [DayOfWeek.Saturday]: 6 }; var NumberToDayOfWeek = { 0: DayOfWeek.Sunday, 1: DayOfWeek.Monday, 2: DayOfWeek.Tuesday, 3: DayOfWeek.Wednesday, 4: DayOfWeek.Thursday, 5: DayOfWeek.Friday, 6: DayOfWeek.Saturday }; var dayOfTheWeek = (date) => { const day = new Date(date).getDay(); return NumberToDayOfWeek[day]; }; var getTodayISOLocal = () => new Date(Date.now() - (/* @__PURE__ */ new Date()).getTimezoneOffset() * 60 * 1e3).toISOString(); function timeSince(date) { const now = /* @__PURE__ */ new Date(); const past = new Date(date); const diffMs = now.getTime() - past.getTime(); const seconds = Math.floor(diffMs / 1e3); const minutes = Math.floor(diffMs / (1e3 * 60)); const hours = Math.floor(diffMs / (1e3 * 60 * 60)); const days = Math.floor(diffMs / (1e3 * 60 * 60 * 24)); const months = Math.floor(diffMs / (1e3 * 60 * 60 * 24 * 30)); const years = Math.floor(diffMs / (1e3 * 60 * 60 * 24 * 365)); return { seconds, minutes, hours, days, months, years }; } function timeAgo(date, { short = false, units = ["years", "months", "days", "hours", "minutes", "seconds"] } = {}) { const diff = timeSince(date); const found = units.find((u) => diff[u] > 0); if (!found) return "now"; const value = diff[found]; if (short) return `${value}${found.slice(0, 1)}`; const unit = found.slice(0, -1); return `${value} ${unit}${value > 1 ? "s" : ""} ago`; } export { elapsedTime, DateUtils, toDateObject, compareDateObject, shortTime, DayOfWeek, DayOfWeekToNumber, NumberToDayOfWeek, dayOfTheWeek, getTodayISOLocal, timeSince, timeAgo };