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.

43 lines (41 loc) • 1.17 kB
import { utcInstantOf } from "../../internal/utcInstant.js"; // src/utc/calculate/endOfQuarterForUtc.ts /** * Return the end of the quarter for a given UTC Instant string. * * - Calculates quarter end month: Q1=3, Q2=6, Q3=9, Q4=12. * - Returns last moment of the quarter. * - Returns "" for invalid input. * * @param value UTC Instant string * @returns UTC Instant string for the end of the quarter, or "" on invalid input * * @example endOfQuarterForUtc("2024-03-15T12:00:00Z") // "2024-03-31T23:59:59.999999999Z" * @example endOfQuarterForUtc("invalid") // "" */ function endOfQuarterForUtc(value) { const instant = utcInstantOf(value); if (instant === null) return ""; try { const zdt = instant.toZonedDateTimeISO("UTC"); const month = zdt.month; const quarterEndMonth = Math.floor((month - 1) / 3) * 3 + 3; return zdt.with({ month: quarterEndMonth, day: 1, hour: 0, minute: 0, second: 0 }).add({ months: 1 }).subtract({ days: 1 }).with({ hour: 23, minute: 59, second: 59, millisecond: 999, microsecond: 999, nanosecond: 999 }).toInstant().toString(); } catch { return ""; } } export { endOfQuarterForUtc };