cycleDateTime
Signature
Section titled “Signature”cycleDateTime(value: string, field: DateTimeCycleField, amount: number, options?: { round?: boolean; overflow?: Overflow; }): stringimport { cycleDateTime } from "@northguild/gmt/plain/calculate";Return a PlainDateTime ISO string with field cycled by amount, wrapping at that field’s own
- min/max instead of carrying into the next larger field.
cycleDateTimeis notaddDateTime: cycling December’smonthby+1stays in the same year, and cyclinghour23by+1stays on the same day. Reach foraddDateTimefor calendar/clock arithmetic; reach forcycleDateTimewhen a single field (e.g. a datepicker segment) must stay isolated from the others.- Date fields (
year/month/day) wrap the same waycycleDatedoes —yearis unbounded,monthwraps1–12,daywraps1–the current month’s day count. Time fields (hour/minute/second/millisecond/microsecond/nanosecond) wrap the same waycycleTimedoes —houralways0–23(nohourCycle: 12option; seecycleTime’s doc for why). - Cycling
monthoryearcan still shiftdayviaoverflow, exactly assetDateTime’s.with()call does. options.roundsteps to the next multiple ofamountin the direction of its sign (ceiling for positive, floor for negative) — not the nearest one. SeecycleDate/cycleTime’s docs for worked examples.- Returns “” for an invalid
valueor an invalidfield.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value |
string |
ISO PlainDateTime string |
field |
DateTimeCycleField |
the field to cycle: “year” | “month” | “day” | “hour” | “minute” | “second” | “millisecond” | “microsecond” | “nanosecond” |
amount |
number |
signed amount to cycle by |
Options
Section titled “Options”options
| Option | Type | Default |
|---|---|---|
round? |
boolean |
— |
overflow? |
Overflow |
— |
Returns
Section titled “Returns”ISO PlainDateTime string with field cycled, or “” on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”cycleDateTime("2024-06-15T09:30:00", "hour", 1) // "2024-06-15T10:30:00"cycleDateTime("2024-12-15T23:30:00", "month", 1) // "2024-01-15T23:30:00" (wraps, stays in the same year)cycleDateTime("2024-12-15T23:30:00", "hour", 1) // "2024-12-15T00:30:00" (wraps, stays on the same day)cycleDateTime("2024-06-15T09:22:00", "minute", 15, { round: true }) // "2024-06-15T09:30:00"cycleDateTime("2024-06-15T09:30:00", "week", 1) // "" ("week" is not a cyclable field)cycleDateTime("invalid", "hour", 1) // ""