@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
23 lines (22 loc) • 1.25 kB
TypeScript
/**
* Return a specific unit extracted from a PlainDateTime string.
*
* - Extracts "year", "month", "day", "week", "dayOfWeek", "hour", "minute", "second", or "millisecond" from a PlainDateTime.
* - Returns zero-padded string for most units.
* - Returns "" for invalid input.
*
* @param value ISO PlainDateTime string
* @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 parseUnitFromDateTime("2024-03-15T14:30:45.123", "year") // "2024"
* @example parseUnitFromDateTime("2024-03-15T14:30:45.123", "week") // "11"
* @example parseUnitFromDateTime("2024-03-15T14:30:45.123", "hour") // "14"
* @example parseUnitFromDateTime("2024-03-15T14:30:45.123", "millisecond") // "123"
* @example parseUnitFromDateTime("invalid", "year") // ""
* @example parseUnitFromDateTime("2024-01-01T00:00:00", "week", { weekStartsOn: "sunday" }) // "1"
*/
export declare function parseUnitFromDateTime(value: string, unit: "year" | "month" | "day" | "week" | "dayOfWeek" | "hour" | "minute" | "second" | "millisecond", optionsArg?: {
weekStartsOn?: "monday" | "sunday";
}): string;