@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.
32 lines (30 loc) • 1.08 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
// src/plain/map/mapDaysInMonth.ts
/**
* Return an array of ISO PlainDate strings for every day in the given year-month.
*
* - Generates array of dates for the specified month.
* - Validates input matches the year-month format exactly.
* - Returns [] for invalid input.
*
* @param month ISO PlainYearMonth string (YYYY-MM)
* @returns array of ISO PlainDate strings for each day in the month
*
* @example mapDaysInMonth("2024-02") // ["2024-02-01", "2024-02-02", ..., "2024-02-29"]
* @example mapDaysInMonth("2024-04") // ["2024-04-01", "2024-04-02", ..., "2024-04-30"]
* @example mapDaysInMonth("invalid") // []
*/
function mapDaysInMonth(month) {
try {
const yearMonth = Temporal.PlainYearMonth.from(month);
if (yearMonth.toString() !== month) return [];
const daysInMonth = yearMonth.daysInMonth;
return Array.from({ length: daysInMonth }, (_, index) => {
const day = (index + 1).toString().padStart(2, "0");
return `${yearMonth.toString()}-${day}`;
});
} catch {
return [];
}
}
export { mapDaysInMonth };