Skip to content

parseUnitFromUtc

parseUnitFromUtc(value: string, unit: UtcUnit, optionsArg?: { weekStartsOn?: "monday" | "sunday"; }): string
import { parseUnitFromUtc } from "@northguild/gmt/utc/parse";

Extract a unit from a UTC datetime string.

  • Valid units: “year”, “month”, “week”, “day”, “dayOfWeek”, “hour”, “minute”, “second”, “millisecond”.
  • Uses Temporal.Instant.from to parse, converts to UTC.
  • Returns “” for invalid input.
Parameter Type Description
value string ISO UTC datetime string (e.g., “2024-03-17T14:30:45Z”)
unit UtcUnit unit to extract from the datetime

options

Option Type Default
weekStartsOn? "monday" | "sunday"

string representation of the requested unit or “” on invalid input

parseUnitFromUtc("2024-03-17T14:30:45Z", "month") // "03"
parseUnitFromUtc("2024-01-01T00:00:00Z", "week") // "1"
parseUnitFromUtc("2024-01-01T00:00:00Z", "week", { weekStartsOn: "sunday" }) // "1"
parseUnitFromUtc("invalid", "month") // ""

utc/parse/parseUnitFromUtc.ts