parseUnitFromDate
Signature
Section titled “Signature”parseUnitFromDate(value: string, unit: "dayOfWeek" | SmallestUnit<"year" | "month" | "week" | "day">, optionsArg?: { weekStartsOn?: "monday" | "sunday"; }): stringReturn a specific date unit extracted from a PlainDate string.
- Extracts “year”, “month”, “day”, “week”, or “dayOfWeek” from a PlainDate.
- Returns zero-padded string for month and day.
unitaccepts the singular or plural name of a Temporal unit ("hour"or"hours"), as Temporal does;"dayOfWeek"has no plural.optionsArgmust be an object or omitted:nullor any other primitive returns "", as Temporal’s GetOptionsObject rejects it.- Returns "" for invalid input.
- The
"year"unit is written as Temporal writes a year: four digits ("2024","0005"), or a sign and six digits outside 0000–9999 ("+010000","-000005").
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value | string | ISO PlainDate string |
unit | "dayOfWeek" | SmallestUnit<"year" | "month" | "week" | "day"> | unit to extract from the date |
Options
Section titled “Options”optionsArg
How weeks are numbered
| Option | Type | Default | Description |
|---|---|---|---|
weekStartsOn? | "monday" | "sunday" | "monday" | The first day of the week, which sets how the "week" unit is numbered. "monday" gives the ISO 8601 week number, where week 1 holds the year’s first Thursday; "sunday" gives the UTS #35 week number with a Sunday first day and one minimal day, where week 1 holds 1 January. Any other value returns "", whatever the unit. |
Returns
Section titled “Returns”string representation of the requested unit or "" on invalid input
Examples
Section titled “Examples”import { parseUnitFromDate } from "@northguild/gmt/plain/parse";
parseUnitFromDate("2024-03-15", "year") // "2024"