Emmanuel Akinfulubi

JavaScriptDate

Get the quarter of a date in JavaScript

Intermediate · 4 min read

Businesses split the year into four quarters of three months each: Q1 is January to March, Q2 is April to June, Q3 is July to September and Q4 is October to December. Sales reports, tax deadlines and dashboards all group dates this way, so "which quarter is this date in?" is a common question in business apps.

In this tutorial you will write quarterOf(date), which returns 1, 2, 3 or 4. Then you will use it to find the first and last day of that quarter. The whole answer is one line of arithmetic, and it works so neatly because JavaScript counts months from 0. You need to know how to make a date with new Date(year, month, day) and that months count from 0.

Integer division with Math.floor

Dividing a month number by 3 tells you how many whole quarters have passed. You only want the whole part, so you round down with Math.floor:

JavaScript

console.log(4 / 3);             // 1.3333333333333333
console.log(Math.floor(4 / 3)); // 1

Math.floor drops everything after the decimal point (for positive numbers). That is called integer division.

Why zero-based months help

getMonth() gives 0 to 11. Divide those by 3 and round down:

  • January, February, March (0, 1, 2) give 0.
  • April, May, June (3, 4, 5) give 1.
  • And so on, up to October, November, December (9, 10, 11), which give 3.

So you get 0 to 3. Quarters are numbered 1 to 4, so you add one at the end.

To print dates, this tutorial uses iso, which turns a date into YYYY-MM-DD text:

JavaScript

const iso = (d) => d.getFullYear() + '-' +
  String(d.getMonth() + 1).padStart(2, '0') + '-' +
  String(d.getDate()).padStart(2, '0');

Here is the exercise. Try it on your own first if you like, then read on and we will build it step by step.

36 Which quarter is this date in?

fair

Return 1 to 4 for a date's calendar quarter, then the first and last day of that quarter.

Stuck? Three hints, smallest first
  1. 1. Three months to a quarter, so integer-divide the zero-based month by 3.
  2. 2. Add one at the end, because quarters are numbered from 1.
  3. 3. The quarter’s first month is (quarter - 1) * 3, and its last day is day 0 of the month after it.

Step 1: the quarter number

Step 1

const quarterOf = (date) => Math.floor(date.getMonth() / 3) + 1;
const may = new Date(2026, 4, 15);      // 15 May 2026
console.log(quarterOf(may));                     // 2
console.log(quarterOf(new Date(2026, 0, 15)));   // 1
console.log(quarterOf(new Date(2026, 11, 25)));  // 4

May is month 4. 4 divided by 3, rounded down, is 1, plus one is 2.

Step 2: check every month

The boundaries are where mistakes hide, so check all twelve months at once. Array.from({ length: 12 }, ...) builds an array of twelve items, calling the function with each index from 0 to 11:

Step 2

const all = Array.from({ length: 12 }, (unused, m) => quarterOf(new Date(2026, m, 1)));
console.log(all.join('')); // '111222333444'

Three of each, in order. March (the last 1) and April (the first 2) fall in the right places.

Step 3: the wrong turn with Math.ceil

If you have worked with month numbers 1 to 12, you may have seen Math.ceil(month / 3), which rounds up. With getMonth(), that goes wrong:

Step 3

const wrong = Array.from({ length: 12 }, (unused, m) => Math.ceil(m / 3));
console.log(wrong.join('')); // '011122233344'

January gives 0, which isn't a quarter, and every boundary is off by one month. Math.ceil is right for 1-to-12 months. Math.floor(...) + 1 is right for the 0-to-11 months that getMonth() gives you.

Step 4: the first day of the quarter

Each quarter starts on months 0, 3, 6 and 9. That is (quarter - 1) * 3:

Step 4

const startOfQuarter = (date) =>
  new Date(date.getFullYear(), (quarterOf(date) - 1) * 3, 1);
console.log(iso(startOfQuarter(may))); // '2026-04-01'

For Q2, (2 - 1) * 3 is 3, which is April. Building the date from parts gives midnight on the 1st.

Step 5: the last day of the quarter

The quarter's last day is day 0 of the month after it ends. Day 0 means "the day before the 1st". The month after Q2 is July, month 6, which is quarter * 3:

Step 5

const endOfQuarter = (date) =>
  new Date(date.getFullYear(), quarterOf(date) * 3, 0);
console.log(iso(endOfQuarter(may)));                     // '2026-06-30'
console.log(iso(endOfQuarter(new Date(2026, 11, 5))));   // '2026-12-31'
console.log(iso(endOfQuarter(new Date(2024, 1, 29))));   // '2024-03-31'

Day 0 of July is 30 June. For Q4, month 12 rolls into January of the next year, and day 0 steps back to 31 December. Q1 always ends on 31 March, leap year or not.

Calendar quarters, not financial ones

These are calendar quarters. Some organizations use a financial year that starts in another month, such as April. Then their Q1 is April to June. You can handle that by shifting the month before dividing, but don't assume the calendar version is the one a report needs. Ask.

The full solution

The panel has quarterOf, startOfQuarter and endOfQuarter. It prints the quarter of 15 May and its first and last days, then checks every month and the ends of Q1 and Q4. Press Run to see the output.

The answer

const iso = (d) => d.getFullYear() + '-' +
  String(d.getMonth() + 1).padStart(2, '0') + '-' +
  String(d.getDate()).padStart(2, '0');

const quarterOf = (date) => Math.floor(date.getMonth() / 3) + 1;

const startOfQuarter = (date) =>
  new Date(date.getFullYear(), (quarterOf(date) - 1) * 3, 1);

const endOfQuarter = (date) =>
  new Date(date.getFullYear(), quarterOf(date) * 3, 0);

const may = new Date(2026, 4, 15);      // 15 May 2026

console.log('15/05/2026 is in Q' + quarterOf(may));
console.log('that quarter runs', iso(startOfQuarter(may)), 'to', iso(endOfQuarter(may)));
console.log('every month:', Array.from({ length: 12 }, (unused, m) => quarterOf(new Date(2026, m, 1))).join(''));

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('January is in Q1', quarterOf(new Date(2026, 0, 15)), 1);
test('March is the last month of Q1', quarterOf(new Date(2026, 2, 31)), 1);
test('April starts Q2', quarterOf(new Date(2026, 3, 1)), 2);
test('December is in Q4', quarterOf(new Date(2026, 11, 25)), 4);
test('all twelve months map cleanly', Array.from({ length: 12 }, (unused, m) => quarterOf(new Date(2026, m, 1))), [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]);
test('Q2 begins on 1 April', iso(startOfQuarter(may)), '2026-04-01');
test('and ends on 30 June', iso(endOfQuarter(may)), '2026-06-30');
test('Q4 ends with the year', iso(endOfQuarter(new Date(2026, 11, 5))), '2026-12-31');
test('Q1 ends on 31 March, leap year or not', iso(endOfQuarter(new Date(2024, 1, 29))), '2024-03-31');

Math.floor(month / 3) + 1 works precisely because getMonth() is zero-based — one of the few places the zero-based month is a convenience rather than a nuisance. If you had a 1-to-12 month you would need Math.ceil(month / 3) instead.

These are calendar quarters. A financial year that starts in April, as the UK's does, has its Q1 running April to June, which is this function with the month shifted before the division. Do not assume the calendar quarter is the one being asked for.

Common mistakes

  • Forgetting the + 1. You get quarters 0 to 3.
  • Using Math.ceil with getMonth(). January becomes quarter 0, and every boundary moves.
  • Using getDate() or getDay(). The quarter depends only on the month.

Try it yourself

  1. Write quarterLabel(date) that returns text like 'Q2 2026'.
  2. Write fiscalQuarterOf(date, startMonth) for a year that starts in startMonth. Hint: shift the month with (month - startMonth + 12) % 12 before dividing.
  3. Count how many days are in each quarter of 2026 using startOfQuarter and endOfQuarter.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.