UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

27 lines (25 loc) • 805 B
import { Temporal } from "../../internal/temporal.js"; import { isValidDateTime } from "../validate/isValidDateTime.js"; // src/plain/parse/parseDayFromDateTime.ts /** * 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") // "" */ function parseDayFromDateTime(value) { if (!isValidDateTime(value)) return ""; try { return Temporal.PlainDateTime.from(value).day.toString().padStart(2, "0"); } catch { return ""; } } export { parseDayFromDateTime };