UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

48 lines (46 loc) • 2.35 kB
import { Temporal } from "../../internal/temporal.js"; import { systemTimeZone } from "../../internal/resolveLocale.js"; import { nowZonedDateTimeISO } from "../../internal/temporalCompat/clock.js"; import { plainDateAdd } from "../../internal/plainDateAdd.js"; import { isDateDurationUnitName } from "../../internal/unitNames.js"; import { readLengthUnits } from "../../internal/lengthFromNow.js"; // src/plain/get/getDateAgo.ts /** * Return the PlainDate before today, as an ISO string, counted from the current time. * * - Reads the clock once, after `units` is checked. * - `units` is an object of unit amounts (years, months, weeks, days), such as `{ days: 90 }`. A * duration string returns "". A time unit returns "", even at zero, as `subtractDate` does; use * `getDateTimeAgo` or `getZonedAgo` for hours. A fractional amount returns "": use whole days. * - Lengths are unsigned: a negative amount returns "". `getDateIn` counts forward. * - `{ days: 0 }` is a valid length and gives today, so * `getDateAgo({ days: 0 })` equals `getToday()`. * - Counts from today in the system time zone, so a server's result changes with its zone; * `getZonedAgo` names one. * - A missing day clamps to the end of the month: 31 March minus a month is * 28 February, or 29 in a leap year. * - Each call reads the clock. To pin the start, use `subtractDate(getToday(), units)`. * - Examples read the clock at 2027-06-15T12:00:00Z, with the system time zone UTC. * * @param units unit amounts, such as `{ days: 90 }`; never negative * @returns ISO PlainDate string, or "" for invalid `units`, a negative amount, a time unit, * an unavailable system zone or a result outside Temporal's range * * @example getDateAgo({ days: 90 }) // "2027-03-17" * @example getDateAgo({ months: 1 }) // "2027-05-15" * @example getDateAgo({ days: -1 }) // "" (lengths are unsigned) * @example getDateAgo({ hours: 36 } as never) // "" (a time unit) */ function getDateAgo(units) { try { const amounts = readLengthUnits(units, isDateDurationUnitName); if (amounts === null) return ""; const timeZone = systemTimeZone(); if (!timeZone) return ""; const today = nowZonedDateTimeISO(timeZone).toPlainDate(); return plainDateAdd(today, Temporal.Duration.from(amounts).negated(), "constrain").toString(); } catch { return ""; } } export { getDateAgo };