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.

58 lines • 3.39 kB
import { Temporal } from "../../internal/temporal.js"; import { DateUnit } from "../../types/date-unit.js"; import "../../types/index.js"; /** * Round an ISO 8601 date string to the specified date unit. * * - Returns "" for invalid inputs. * - All date units use manual start-of-unit rounding. Weeks start on Monday. * - The rounding grid is anchored at the unit containing `value`, so `"halfEven"` breaks an exact * tie towards that unit's start (multiple 0, the even one). * - The position within the unit is measured towards the next start, so a date in a unit that * began before the first representable date (`-271821-04-19`) still rounds up to the next start. * When it rounds down to that unrepresentable start, the result is "". * - In the last representable year (`+275760`) a date that rounds down still returns its unit's * start; only rounding up past `+275760-09-13` returns "". * - Wraps all Temporal calls in try-catch; returns "" on any error. * * @param value ISO 8601 date string * @param options The unit to round to and how to round * @returns Rounded ISO 8601 date string, or "" on invalid input * * @example roundDate("2024-06-15", { smallestUnit: "year" }) // "2024-01-01" * @example roundDate("2024-06-15", { smallestUnit: "month" }) // "2024-06-01" (14 of 30 days in: rounds down) * @example roundDate("2024-06-16", { smallestUnit: "month" }) // "2024-07-01" (half way: rounds up) * @example roundDate("2024-06-15", { smallestUnit: "week" }) // "2024-06-17" (a Saturday: the next Monday is closer) * @example roundDate("2024-06-15", { smallestUnit: "day" }) // "2024-06-15" * @example roundDate("2024-06-15", { smallestUnit: "months" }) // "2024-06-01" (plural unit name) * @example roundDate("-271821-04-19", { smallestUnit: "month" }) // "-271821-05-01" (the month began before the range) * @example roundDate("+275760-06-15", { smallestUnit: "year", roundingMode: "floor" }) // "+275760-01-01" (the next year is past the range) * @example roundDate("+275760-06-15", { smallestUnit: "year", roundingMode: "ceil" }) // "" (+275761-01-01 is past the range) * @example roundDate("2024-05-20", { smallestUnit: "month", roundingIncrement: 1.5 }) // "2024-06-01" (the increment truncates to 1) * @example roundDate("2024-05-20", { smallestUnit: "month", roundingMode: "bogus" as never }) // "" * @example roundDate("invalid", { smallestUnit: "year" }) // "" */ export declare function roundDate(value: string, options: { /** * The unit to round to: `"year"`, `"month"`, `"week"` or `"day"`, singular or plural, as * Temporal's GetTemporalUnitValuedOption accepts both (§13.17). A time unit such as `"hour"` * returns `""`. */ smallestUnit: Temporal.SmallestUnit<DateUnit>; /** * How many units make one rounding step, counted from the unit that holds `value`. Read as * Temporal reads it: a non-integer is truncated (`1.5` rounds by 1), and a value below 1 or not * finite returns `""`. * * @defaultValue `1`, Temporal's default. */ roundingIncrement?: number; /** * Which way a date between two steps goes: one of Temporal's nine rounding modes, such as * `"floor"`, `"ceil"` or `"halfExpand"` (to the nearer step, a tie going up). Any other value * returns `""`. * * @defaultValue `"halfExpand"`, Temporal's default. */ roundingMode?: Temporal.RoundingMode; }): string;