Get the Quarter of the Year from a Date in JavaScript
To get the quarter, compute Math.ceil(plainDate.month / 3) — 1 for Jan-Mar through 4 for Oct-Dec. Part of Date Arithmetic Without Mutations.
Why this scenario is tricky
The only sharp edge here is month indexing: Temporal.PlainDate.month is 1-based (1 = January), so Math.ceil(month / 3) gives the quarter directly. Do the same maths on a legacy Date and getMonth()'s 0-based value shifts every quarter by one.
Minimal working solution
import { Temporal } from '@js-temporal/polyfill';
const q = Math.ceil(Temporal.PlainDate.from('2024-05-15').month / 3); // 2
Full production version
import { Temporal } from '@js-temporal/polyfill';
function quarterBounds(d: Temporal.PlainDate) {
const q = Math.ceil(d.month / 3);
const start = Temporal.PlainDate.from({ year: d.year, month: (q - 1) * 3 + 1, day: 1 });
return { quarter: q, start, end: start.add({ months: 3 }) }; // half-open
}
Verification snippet
Common pitfalls
Frequently Asked Questions
How do I get the quarter of the year from a date?
Compute Math.ceil(plainDate.month / 3) on a Temporal.PlainDate. Because Temporal months are 1-based, this returns 1 for January-March up to 4 for October-December without any adjustment.
How do I get the start and end of a quarter?
Derive the quarter with Math.ceil(month / 3), build a PlainDate for day 1 of that quarter's first month ((q-1)*3+1), and add three months for the exclusive end. Query the half-open range [start, end) for the reporting period.