cycleDateTime
Signature
Section titled “Signature”cycleDateTime(value: string, field: DateTimeCycleField, amount: number, options?: { round?: boolean; overflow?: "reject" | "constrain" | undefined; }): stringReturn 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. - Returns "" for an invalid
value, an invalidfield, or anamountthat is not a finite number.
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
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 PlainDateTime string with field cycled, or "" on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”import { cycleDateTime } from "@northguild/gmt/plain/calculate";
cycleDateTime("2024-06-15T09:30:00", "hour", 1) // "2024-06-15T10:30:00"