@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) • 698 B
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDate } from "../validate/isValidDate.js";
// src/plain/parse/parseMonthFromDate.ts
/**
* Return the month (1-12) for a given ISO 8601 date string.
*
* - Returns zero-padded string for month.
* - Returns "" for invalid input.
*
* @param value ISO date string
* @returns Month (01-12) or "" on invalid input
*
* @example parseMonthFromDate("2024-03-15") // "03"
* @example parseMonthFromDate("invalid") // ""
*/
function parseMonthFromDate(value) {
if (!isValidDate(value)) return "";
try {
return Temporal.PlainDate.from(value).month.toString().padStart(2, "0");
} catch {
return "";
}
}
export { parseMonthFromDate };