@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.
37 lines (35 loc) • 1.57 kB
JavaScript
import { plainMonthDayOf } from "../../internal/inputChecks.js";
// src/plain/convert/monthDayToDate.ts
/**
* The ISO date of a month-day in a given year.
*
* - `--02-29` in a year that is not a leap year gives 28 February. This is the ~constrain~ step
* of Temporal's `PlainMonthDay.prototype.toPlainDate` (CalendarDateFromFields with
* ~constrain~). The result is never 1 March and never `""`.
* - `year` must be an integer. A fractional, string or `bigint` year gives `""`; Temporal would
* truncate or coerce it, and gmt does not.
* - Only the ISO calendar is read; a month-day with another calendar annotation gives `""`.
*
* @param monthDay a month-day, `--MM-DD` or `MM-DD`
* @param year an integer, as Temporal's ISO calendar counts it: 0 is 1 BCE and -1 is 2 BCE
* @returns the ISO date, or `""` when `monthDay` is not a valid month-day, `year` is not an
* integer, or the date falls outside the PlainDate range
*
* @example monthDayToDate("--12-25", 2024) // "2024-12-25"
* @example monthDayToDate("--02-29", 2024) // "2024-02-29"
* @example monthDayToDate("--02-29", 2023) // "2023-02-28" (constrained)
* @example monthDayToDate("--02-30", 2024) // ""
* @example monthDayToDate("--12-25", 2024.5) // ""
* @example monthDayToDate("--09-14", 275760) // "" (above the PlainDate range)
*/
function monthDayToDate(monthDay, year) {
if (!Number.isInteger(year)) return "";
const pmd = plainMonthDayOf(monthDay);
if (pmd === null) return "";
try {
return pmd.toPlainDate({ year }).toString();
} catch {
return "";
}
}
export { monthDayToDate };