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) • 807 B
import { Temporal } from "../../internal/temporal.js"; import { isValidDateTime } from "../validate/isValidDateTime.js"; // src/plain/parse/parseHourFromDateTime.ts /** * Return the hour (00-23) for a given ISO 8601 datetime string. * * - Returns zero-padded string for hour. * - Returns "" for invalid input. * * @param value ISO datetime string * @returns Hour (00-23) or "" on invalid input * * @example parseHourFromDateTime("2024-03-15T14:30:00") // "14" * @example parseHourFromDateTime("2024-03-15T00:00:00") // "00" * @example parseHourFromDateTime("invalid") // "" */ function parseHourFromDateTime(value) { if (!isValidDateTime(value)) return ""; try { return Temporal.PlainDateTime.from(value).hour.toString().padStart(2, "0"); } catch { return ""; } } export { parseHourFromDateTime };