@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.
28 lines (26 loc) • 880 B
JavaScript
import { zonedDateTimeOf } from "../../internal/zonedWallClock.js";
// src/zoned/calculate/getQuarterForZoned.ts
/**
* Return the quarter of the year (1-4) for a given zoned ISO datetime.
*
* - Q1 = months 1-3, Q2 = months 4-6, Q3 = months 7-9, Q4 = months 10-12.
* - Validation is performed on the input.
*
* @param value ISO ZonedDateTime string
* @returns number (1-4) or null on invalid input
*
* @example getQuarterForZoned("2024-02-15T14:30:00+00:00[UTC]") // 1
* @example getQuarterForZoned("2024-05-10T10:00:00+00:00[UTC]") // 2
* @example getQuarterForZoned("2024-11-20T08:00:00+00:00[UTC]") // 4
* @example getQuarterForZoned("invalid") // null
*/
function getQuarterForZoned(value) {
const zdt = zonedDateTimeOf(value);
if (zdt === null) return null;
try {
return Math.floor((zdt.month - 1) / 3) + 1;
} catch {
return null;
}
}
export { getQuarterForZoned };