@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.
30 lines (28 loc) • 982 B
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDate } from "../validate/isValidDate.js";
// src/plain/calculate/getDaysInYear.ts
/**
* Return the number of days in the calendar year containing `value`.
*
* - 365 for a common year, 366 for a leap year.
* - It measures the calendar year that holds the date, not the next 12 months. From 2027-06-15 it
* returns `365`, while `totalUnitsFromNow({ years: 1 }, "days")` returns `366`: the next 12
* months from that date take in 29 February 2028.
* - Returns null on invalid input.
*
* @param value ISO PlainDate string
* @returns 365 or 366, or null on invalid input
*
* @example getDaysInYear("2024-06-15") // 366 (leap year)
* @example getDaysInYear("2023-06-15") // 365
* @example getDaysInYear("invalid") // null
*/
function getDaysInYear(value) {
if (!isValidDate(value)) return null;
try {
return Temporal.PlainDate.from(value).daysInYear;
} catch {
return null;
}
}
export { getDaysInYear };