@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.
26 lines (24 loc) • 705 B
JavaScript
import { plainDateTimeOf } from "../../internal/inputChecks.js";
// src/plain/parse/parseMonthFromDateTime.ts
/**
* Return the month (1-12) for a given ISO 8601 datetime string.
*
* - Returns zero-padded string for month.
* - Returns "" for invalid input.
*
* @param value ISO datetime string
* @returns Month (01-12) or "" on invalid input
*
* @example parseMonthFromDateTime("2024-03-15T12:30:00") // "03"
* @example parseMonthFromDateTime("invalid") // ""
*/
function parseMonthFromDateTime(value) {
const dateTime = plainDateTimeOf(value);
if (dateTime === null) return "";
try {
return dateTime.month.toString().padStart(2, "0");
} catch {
return "";
}
}
export { parseMonthFromDateTime };