@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.
40 lines (38 loc) • 1.25 kB
JavaScript
import { utcInstantOf } from "../../internal/utcInstant.js";
// src/utc/calculate/startOfQuarterForUtc.ts
/**
* Return the start of the quarter for a given UTC datetime string.
*
* - Calculates quarter start month: Q1=1, Q2=4, Q3=7, Q4=10.
* - Resets every time field, including milliseconds, microseconds and nanoseconds.
* - Returns "" for invalid input.
*
* @param value ISO UTC datetime string (e.g. "2024-03-15T12:00:00Z")
* @returns ISO UTC datetime string representing the start of the quarter, or "" on invalid input
*
* @example startOfQuarterForUtc("2024-03-15T12:00:00Z") // "2024-01-01T00:00:00Z"
* @example startOfQuarterForUtc("2024-05-15T12:34:56.789Z") // "2024-04-01T00:00:00Z"
* @example startOfQuarterForUtc("invalid") // ""
*/
function startOfQuarterForUtc(value) {
const instant = utcInstantOf(value);
if (instant === null) return "";
try {
const zdt = instant.toZonedDateTimeISO("UTC");
const month = zdt.month;
const quarterStartMonth = Math.floor((month - 1) / 3) * 3 + 1;
return zdt.with({
month: quarterStartMonth,
day: 1,
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
microsecond: 0,
nanosecond: 0
}).toInstant().toString();
} catch {
return "";
}
}
export { startOfQuarterForUtc };