@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
22 lines (21 loc) • 1.09 kB
TypeScript
export type UtcUnit = "year" | "month" | "week" | "day" | "dayOfWeek" | "hour" | "minute" | "second" | "millisecond";
/**
* 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.
*
* @param value ISO UTC datetime string (e.g., "2024-03-17T14:30:45Z")
* @param unit unit to extract from the datetime
* @param optionsArg optional: weekStartsOn ("monday" | "sunday") for week calculations
* @returns string representation of the requested unit or "" on invalid input
*
* @example parseUnitFromUtc("2024-03-17T14:30:45Z", "month") // "03"
* @example parseUnitFromUtc("2024-01-01T00:00:00Z", "week") // "1"
* @example parseUnitFromUtc("2024-01-01T00:00:00Z", "week", { weekStartsOn: "sunday" }) // "1"
* @example parseUnitFromUtc("invalid", "month") // ""
*/
export declare function parseUnitFromUtc(value: string, unit: UtcUnit, optionsArg?: {
weekStartsOn?: "monday" | "sunday";
}): string;