UNPKG

@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) • 844 B
import { Temporal } from "../../internal/temporal.js"; import { isValidDate } from "../validate/isValidDate.js"; // src/plain/calculate/getDayOfYear.ts /** * Return the ordinal day-of-year (1-based) for `value`. * * - Jan 1 is 1; Dec 31 is 365 (common year) or 366 (leap year). * - Returns null on invalid input. * * @param value ISO PlainDate string * @returns 1-366, or null on invalid input * * @example getDayOfYear("2024-01-01") // 1 * @example getDayOfYear("2024-12-31") // 366 (leap year) * @example getDayOfYear("2023-12-31") // 365 * @example getDayOfYear("2024-03-01") // 61 (after the Feb 29 leap day) * @example getDayOfYear("invalid") // null */ function getDayOfYear(value) { if (!isValidDate(value)) return null; try { return Temporal.PlainDate.from(value).dayOfYear; } catch { return null; } } export { getDayOfYear };