intervalFromDurationDateTime
Signature
Section titled “Signature”intervalFromDurationDateTime(value: string, duration: string, anchor: "start" | "end", options?: { overflow?: Overflow; }): { start: string; end: string; }import { intervalFromDurationDateTime } from "@northguild/gmt/plain/interval";Construct a datetime interval from a single point plus an ISO 8601 duration, anchored at either end.
anchor: "start"treatsvalueas the interval start and addsdurationto get the end.anchor: "end"treatsvalueas the interval end and subtractsdurationto get the start.- Uses
Temporal.PlainDateTime.prototype.add/.subtract, so calendar units (years/months/weeks) resolve againstvalueitself — no separaterelativeTois needed, unlikeaddDuration. - A negative
duration(e.g."-P1D") can invert the computed span; returns null when that happens, mirroringintervalIntersectionDateTime’sstart > endrejection. overflow(“constrain” (default) | “reject”) controls out-of-range results, e.g. adding 1 month to Jan 31: “constrain” clamps to Feb 29/28, “reject” returns null.- Returns null on invalid input (unparseable
value, invalidduration, or ananchorother than"start"/"end").
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value |
string |
ISO PlainDateTime string |
duration |
string |
ISO 8601 duration string |
anchor |
"start" | "end" |
“start” | “end” — which endpoint value represents |
Options
Section titled “Options”options
| Option | Type | Default |
|---|---|---|
overflow? |
Overflow |
— |
Returns
Section titled “Returns”{ start, end } with the constructed span, or null on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”intervalFromDurationDateTime("2024-01-01T00:00:00", "P1DT2H", "start") // { start: "2024-01-01T00:00:00", end: "2024-01-02T02:00:00" }intervalFromDurationDateTime("2024-01-02T02:00:00", "P1DT2H", "end") // { start: "2024-01-01T00:00:00", end: "2024-01-02T02:00:00" }intervalFromDurationDateTime("2024-01-31T12:00:00", "P1M", "start", { overflow: "reject" }) // nullintervalFromDurationDateTime("2024-01-05T00:00:00", "-P10D", "start") // null (inverted span)intervalFromDurationDateTime("invalid", "P1D", "start") // null