UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

31 lines (29 loc) 1.08 kB
import { CachedIntl } from "../utils/intl.mjs"; import configuration from "@intlayer/config/built"; //#region src/formatters/relativeTime.ts /** * Calculate the difference between 2 dates in the given unit. */ const diffInUnit = (from, to, unit) => { const sec = (to.getTime() - from.getTime()) / 1e3; switch (unit) { case "second": return sec; case "minute": return sec / 60; case "hour": return sec / 3600; case "day": return sec / 86400; case "month": return sec / (30 * 86400); case "quarter": return sec / (90 * 86400); case "year": return sec / (365 * 86400); default: return sec; } }; const relativeTime = (from, to = /* @__PURE__ */ new Date(), options) => { const fromDate = new Date(from); const toDate = new Date(to); const unit = options?.unit ?? "second"; const value = diffInUnit(fromDate, toDate, unit); return new CachedIntl.RelativeTimeFormat(options?.locale ?? configuration?.internationalization?.defaultLocale, options).format(Math.round(value), unit); }; //#endregion export { relativeTime }; //# sourceMappingURL=relativeTime.mjs.map