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.

54 lines • 3.21 kB
import { FiscalYearLabel } from "../../types/fiscal-calendar.js"; import "../../types/index.js"; /** * Return the quarter `value` falls in, as a year and a quarter number. * * - With no options these are the calendar quarters: Q1 is January to March, and `year` is * the calendar year. * - With `options`, these are fiscal quarters. The fiscal year starts on day 1 of * `fiscalYearStartMonth`, and `yearLabel` names it: `"startYear"`, `"endYear"`, or * `"majorityYear"` (the calendar year holding the most of its days). Both are required, and * there is no default. Start months 1 to 7 put most days in the start year, and 8 to 12 put * most in the end year. * - `value` must be zoneless — an ISO date or datetime, as `isValidIsoDateLike` accepts. See * `getIsoWeekDate` for why a moment is not accepted here. * - Returns null on invalid input, including `options` without both fields, a * `fiscalYearStartMonth` that is not an integer from 1 to 12, or an unrecognised `yearLabel`. * * @param value zoneless ISO 8601 date or datetime string (e.g. "2024-06-15") * @param optionsArg The fiscal year's start month and the rule that names it * @returns { year, quarter }, or null on invalid input * * @example getQuarter("2024-06-15") // { year: 2024, quarter: 2 } * @example getQuarter("2024-10-01") // { year: 2024, quarter: 4 } * @example getQuarter("2024-04-01", { fiscalYearStartMonth: 4, yearLabel: "startYear" }) // { year: 2024, quarter: 1 } * @example getQuarter("2024-03-31", { fiscalYearStartMonth: 4, yearLabel: "startYear" }) // { year: 2023, quarter: 4 } (the previous fiscal year) * @example getQuarter("2024-03-31", { fiscalYearStartMonth: 4, yearLabel: "endYear" }) // { year: 2024, quarter: 4 } * @example getQuarter("2023-10-15", { fiscalYearStartMonth: 7, yearLabel: "endYear" }) // { year: 2024, quarter: 2 } * @example getQuarter("2023-10-15", { fiscalYearStartMonth: 7, yearLabel: "majorityYear" }) // { year: 2023, quarter: 2 } (July to December is 184 of its days) * @example getQuarter("2024-10-01", { fiscalYearStartMonth: 10, yearLabel: "majorityYear" }) // { year: 2025, quarter: 1 } * @example getQuarter("2024-06-15", { fiscalYearStartMonth: 4 }) // null (no yearLabel) * @example getQuarter("2024-06-15", { fiscalYearStartMonth: 13, yearLabel: "startYear" }) // null * @example getQuarter("2024-06-15T12:00:00Z") // null (a moment, not a calendar date) * @example getQuarter("invalid") // null */ export declare function getQuarter(value: string, optionsArg?: { /** * The month the fiscal year starts in, 1 (January) through 12. A date before it belongs to * the previous fiscal year, so with `4` the date `"2024-03-31"` is in that year's Q4. */ fiscalYearStartMonth: number; /** The calendar year that names a fiscal year. See `FiscalYearLabel`. */ yearLabel: FiscalYearLabel; }): { /** * The year that names the quarter's year: the calendar year with no `options`, otherwise the * one `yearLabel` picks. */ year: number; /** * The quarter of that year, 1 through 4. Quarter 1 begins in January with no `options`, and * in the `fiscalYearStartMonth` month otherwise. */ quarter: number; } | null;