@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.
36 lines (34 loc) • 1.85 kB
JavaScript
import { isOptionsArgument } from "../../internal/isObject.js";
import { startOrEndOfUtc } from "../../internal/startOrEndOfUtc.js";
// src/utc/calculate/endOfUtc.ts
/**
* Return the end of the specified date-time `unit` for a given UTC datetime string.
*
* - Converts to ZonedDateTime, sets to end of unit, converts back to Instant.
* - Supports: "year", "month", "week", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond", each also in its plural form (`"days"`), as Temporal accepts.
* - The end is the last nanosecond of the unit, one nanosecond before the next unit starts.
* - Returns "" for invalid input.
* - **Compatibility:** before 1.16.0 the default printed only the digits the unit names — none for
* `second` and coarser, 3 for `millisecond`, 6 for `microsecond` — which wrote a moment earlier
* than the end. Pass that `fractionalSecondDigits` to keep the previous string.
*
* @param value ISO UTC datetime string
* @param unit date or time unit, singular or plural, to specify the end
* @param options Where a week starts, and the precision of the result
* @returns UTC Instant string representing the end of the unit, or "" on invalid input
*
* @example endOfUtc("2024-03-15T14:30:45Z", "year") // "2024-12-31T23:59:59.999999999Z"
* @example endOfUtc("2024-03-15T14:30:45Z", "month", { fractionalSecondDigits: 0 }) // "2024-03-31T23:59:59Z" — the pre-1.16.0 string
* @example endOfUtc("2024-03-15T14:30:45Z", "month") // "2024-03-31T23:59:59.999999999Z"
* @example endOfUtc("2024-03-15T14:30:45Z", "hours") // "2024-03-15T14:59:59.999999999Z"
* @example endOfUtc("invalid", "year") // ""
*/
function endOfUtc(value, unit, options) {
try {
if (!isOptionsArgument(options)) return "";
return startOrEndOfUtc(value, unit, options ?? {}, true);
} catch {
return "";
}
}
export { endOfUtc };