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.

35 lines (33 loc) • 1.17 kB
import { plainDateOf } from "../../internal/inputChecks.js"; // src/plain/calculate/startOfQuarterForDate.ts /** * Return the first day of the quarter for a given ISO date. * * - Returns the first day (month and day set to 1) of the quarter containing the input date. * - Q1 returns "01-01", Q2 returns "04-01", Q3 returns "07-01", Q4 returns "10-01". * - Validates input using isValidDate. * * @param value ISO PlainDate string * @returns ISO PlainDate string for the first day of the quarter, or "" on invalid input * * @example startOfQuarterForDate("2024-03-15") // "2024-01-01" * @example startOfQuarterForDate("2024-06-15") // "2024-04-01" * @example startOfQuarterForDate("2024-09-15") // "2024-07-01" * @example startOfQuarterForDate("2024-12-15") // "2024-10-01" * @example startOfQuarterForDate("invalid") // "" */ function startOfQuarterForDate(value) { const date = plainDateOf(value); if (date === null) return ""; try { const month = date.month; const quarterStartMonth = Math.floor((month - 1) / 3) * 3 + 1; return date.with({ month: quarterStartMonth, day: 1 }).toString(); } catch { return ""; } } export { startOfQuarterForDate };