cycleDate
Signature
Section titled “Signature”cycleDate(value: string, field: DateCycleField, amount: number, options?: { round?: boolean; overflow?: Overflow; }): stringimport { cycleDate } from "@northguild/gmt/plain/calculate";Return 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 rounding, see below).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. options.rounddoes not round to the nearest increment — it steps to the next multiple ofamountin the direction of its sign (ceiling for positive, floor for negative), matching@internationalized/date’sCycleOptions.round. E.g. cyclingyear2022by+5withround: truelands on2025(the next multiple of 5 above 2022), not2020(the nearest multiple).- Returns “” for an invalid
valueor an invalidfield.
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
| Option | Type | Default |
|---|---|---|
round? |
boolean |
— |
overflow? |
Overflow |
— |
Returns
Section titled “Returns”ISO PlainDate string with field cycled, or “” on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”cycleDate("2024-06-15", "month", 1) // "2024-07-15"cycleDate("2024-12-15", "month", 1) // "2024-01-15" (wraps, stays in the same year)cycleDate("2024-12-31", "day", 1) // "2024-12-01" (wraps within the same month)cycleDate("2024-01-15", "month", 13) // "2024-02-15" (amount larger than the range)cycleDate("2024-01-31", "month", 1) // "2024-02-29" (constrain clamps the day)cycleDate("2024-01-31", "month", 1, { overflow: "reject" }) // ""cycleDate("2022-02-03", "year", 5, { round: true }) // "2025-02-03"cycleDate("2024-06-15", "week", 1) // "" ("week" is not a cyclable date field)cycleDate("invalid", "month", 1) // ""