@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.
29 lines (27 loc) • 870 B
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDate } from "../validate/isValidDate.js";
// src/plain/calculate/getDaysInMonth.ts
/**
* Return the number of days in the month containing `value`.
*
* - 28–31 depending on the month; February varies by leap year.
* - Returns null on invalid input.
*
* @param value ISO PlainDate string
* @returns number of days in the month (28-31), or null on invalid input
*
* @example getDaysInMonth("2024-02-15") // 29 (leap year)
* @example getDaysInMonth("2023-02-15") // 28
* @example getDaysInMonth("2024-04-01") // 30
* @example getDaysInMonth("2024-01-01") // 31
* @example getDaysInMonth("invalid") // null
*/
function getDaysInMonth(value) {
if (!isValidDate(value)) return null;
try {
return Temporal.PlainDate.from(value).daysInMonth;
} catch {
return null;
}
}
export { getDaysInMonth };