UNPKG

@burglekitt/gmt

Version:

Temporal-based date and time utilities with timezone support and polyfill integration

27 lines (26 loc) 803 B
import { Temporal } from "@js-temporal/polyfill"; import { isValidDateTime } from "../validate/index.js"; /** * Return the day of month (1-31) for a given ISO 8601 datetime string. * * - Returns zero-padded string for day. * - Returns "" for invalid input. * * @param value ISO datetime string * @returns Day (01-31) or "" on invalid input * * @example parseDayFromDateTime("2024-03-15T12:30:00") // "15" * @example parseDayFromDateTime("2024-12-31T23:59:59") // "31" * @example parseDayFromDateTime("invalid") // "" */ export function parseDayFromDateTime(value) { if (!isValidDateTime(value)) return ""; try { const dateTime = Temporal.PlainDateTime.from(value); return dateTime.day.toString().padStart(2, "0"); } catch { return ""; } }