cycleDate
Signature
Section titled “Signature”cycleDate(value: string, field: DateCycleField, amount: number, options?: { round?: boolean; overflow?: "reject" | "constrain" | undefined; }): stringReturn a PlainDate ISO string with field cycled by amount, wrapping at that field’s own min/max instead of carrying into the next larger field.
cycleDateis notaddDate: cycling December’smonthby+1stays in the same year ("2024-12-15"→"2024-01-15"), whereaddDate(value, { months: 1 })would correctly overflow into January of the next year. Reach foraddDatewhen you want calendar arithmetic; reach forcycleDatewhen a single field (e.g. a datepicker segment) must stay isolated from the others.yearhas no upper/lower wrap — cycling it is plain addition (or a step to a multiple, withround):2022by+5withround: truelands on2025, the next multiple of 5.monthwraps1–12.daywraps1–the current month’s day count, so cyclingdaynever changesmonth.- Cycling
monthoryearcan still shiftdayviaoverflow— e.g. cyclingmonthfrom a 31st into a shorter month clamps under the default"constrain"(or returns""under"reject") exactly the waysetDate’s own.with()call does; this is the same clampingaddDate’s Jan 31 + 1 month case produces, not new behavior. - Returns "" for an invalid
value, an invalidfield, or anamountthat is not a finite number.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value | string | ISO PlainDate string |
field | DateCycleField | the field to cycle: “year” | “month” | “day” |
amount | number | signed amount to cycle by |
Options
Section titled “Options”options
How the field is stepped and an out-of-range day is handled
| Option | Type | Default | Description |
|---|---|---|---|
round? | boolean | false | Whether to step to a multiple of amount instead of adding it. true moves to the next multiple in the direction of the sign of amount (the ceiling for a positive amount, the floor for a negative one), not to the nearest one; false adds amount. |
overflow? | "reject" | "constrain" (Overflow) | "constrain", Temporal’s default. | What to do when the cycled month or year leaves day past the end of its month. "constrain" clamps day to the last day of that month; "reject" returns "". |
Returns
Section titled “Returns”ISO PlainDate string with field cycled, or "" on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”import { cycleDate } from "@northguild/gmt/plain/calculate";
cycleDate("2024-06-15", "month", 1) // "2024-07-15"