@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.
44 lines (42 loc) • 1.4 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDateTime } from "../validate/isValidDateTime.js";
// src/plain/calculate/endOfQuarterForDateTime.ts
/**
* Return the end of the quarter for a given ISO datetime.
*
* - Returns the last moment of the quarter with time set to 23:59:59.999999999.
* - Q1 ends on "03-31T23:59:59.999999999", Q2 ends on "06-30T23:59:59.999999999", etc.
* - Validates input using isValidDateTime.
*
* @param value ISO PlainDateTime string
* @returns ISO PlainDateTime string for the end of the quarter, or "" on invalid input
*
* @example endOfQuarterForDateTime("2024-03-15T12:00:00") // "2024-03-31T23:59:59.999999999"
* @example endOfQuarterForDateTime("2024-06-15T12:00:00") // "2024-06-30T23:59:59.999999999"
* @example endOfQuarterForDateTime("invalid") // ""
*/
function endOfQuarterForDateTime(value) {
if (!isValidDateTime(value)) return "";
try {
const dateTime = Temporal.PlainDateTime.from(value);
const month = dateTime.month;
const quarterEndMonth = Math.floor((month - 1) / 3) * 3 + 3;
return dateTime.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
}).toString();
} catch {
return "";
}
}
export { endOfQuarterForDateTime };