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.

33 lines • 1.79 kB
import { DateDurationUnit } from "../../types/date-duration-unit.js"; import { Overflow } from "../../types/overflow.js"; import "../../types/index.js"; /** * Return a PlainDate ISO string with `units` subtracted from `value`. * * - Validates `value`, `units`, and `amount` before performing the subtract. * - Accepts an RFC 9557 calendar-annotated PlainDate string (as `convertDateToCalendar` writes it), * not just a bare ISO string. See `addDate`'s JSDoc for the calendar-unit arithmetic (leap * months and years, the `overflow` asymmetry); this function is the mirror image. * - Compatibility: since 1.16.0 calendar strings are RFC 9557 (ISO digits, `[u-ca=<id>]`, canonical * calendar ids); see `isValidCalendarDate`. * - Returns "" for invalid inputs. * * @param value ISO PlainDate string, optionally calendar-annotated * @param units Partial<Record<DateDurationUnit, number>> object specifying units to subtract * @param options How an out-of-range result is handled * @returns ISO PlainDate string after subtraction, or "" on invalid input * * @example subtractDate("2024-03-15", { days: 5 }) // "2024-03-10" * @example subtractDate("invalid", { days: 5 }) // "" * @example subtractDate("2024-03-31", { months: 1 }, { overflow: "reject" }) // "" * @example subtractDate("2024-03-25[u-ca=hebrew]", { months: 1 }) // "2024-02-24[u-ca=hebrew]" (15 Adar II 5784 -> 15 Adar I) */ export declare function subtractDate(value: string, units: Partial<Record<DateDurationUnit, number>>, options?: { /** * What to do when the result is not a real date. `"constrain"` clamps it to the last valid day, * so Mar 31 - 1 month is Feb 29 or 28; `"reject"` returns `""`. * * @defaultValue `"constrain"`, Temporal's default. */ overflow?: Overflow; }): string;