Skip to main content

How to Get the Quarter from a Date in JavaScript

In business and financial contexts, it's often necessary to determine which quarter of the year a specific date falls into. A quarter is a three-month period, and a year is typically divided into four quarters.

This guide will teach you a simple and reliable mathematical trick to get the quarter (1, 2, 3, or 4) from any JavaScript Date object.

The Core Concept: The Four Quarters of a Year

A standard calendar year is divided into four quarters:

  • Q1: January, February, March
  • Q2: April, May, June
  • Q3: July, August, September
  • Q4: October, November, December

Our goal is to write a function that takes a Date object and returns the corresponding quarter number (1, 2, 3, or 4).

The Solution: A Reusable getQuarter Function

The logic for this conversion can be encapsulated in a clean, one-line function.

For example, you have a Date object and you want to find out which quarter it belongs to.

// Problem: How to determine the quarter for this date?
const myDate = new Date('2025-08-15'); // August 15th

This function uses a simple mathematical formula based on the month's index.

/**
* Returns the quarter of the year (1-4) for a given date.
* @param {Date} date - The date to get the quarter for.
* @returns {number} The quarter number (1, 2, 3, or 4).
*/
function getQuarter(date = new Date()) {
const month = date.getMonth(); // getMonth() returns 0-11
return Math.floor(month / 3) + 1;
}

// Example Usage:
const myDate = new Date('2025-08-15'); // August is month index 7
console.log(`The date is in Q${getQuarter(myDate)}`); // Output: The date is in Q3

console.log(`Q1 Test (Feb):`, getQuarter(new Date('2025-02-10'))); // Output: Q1 Test (Feb): 1
console.log(`Q2 Test (Jun):`, getQuarter(new Date('2025-06-20'))); // Output: Q2 Test (Jun): 2
console.log(`Q4 Test (Dec):`, getQuarter(new Date('2025-12-31'))); // Output: Q4 Test (Dec): 4

How the Logic Works

The function hinges on two key JavaScript features:

  1. date.getMonth(): This method returns a zero-based index for the month, where 0 is January and 11 is December.
  2. Math.floor(): This function rounds a number down to the nearest whole number.

Let's trace the logic for our example date, August 15th (2023-08-15):

  1. date.getMonth() returns 7 (for August).
  2. We divide this by 3: 7 / 3 = 2.333....
  3. Math.floor(2.333...) rounds this down to 2.
  4. Finally, we add 1: 2 + 1 = 3.

The date is correctly identified as being in the 3rd quarter. This simple formula works for every month of the year.

Practical Example: Categorizing Dates by Quarter

This script demonstrates a real-world use case where an array of dates is processed and each date is categorized by its quarter.

const salesData = [
new Date('2025-01-20'),
new Date('2025-05-15'),
new Date('2025-11-05'),
new Date('2025-08-30'),
];

const salesByQuarter = {
Q1: [],
Q2: [],
Q3: [],
Q4: [],
};

salesData.forEach(saleDate => {
const quarter = getQuarter(saleDate);
const quarterKey = `Q${quarter}`;
salesByQuarter[quarterKey].push(saleDate);
});

console.log(salesByQuarter);

Output:

{
Q1: [ Date('2025-01-20') ],
Q2: [ Date('2025-05-15') ],
Q3: [ Date('2025-08-30') ],
Q4: [ Date('2025-11-05') ]
}

Conclusion

Getting the quarter from a Date object is a straightforward task in JavaScript using a simple mathematical formula.

  • The recommended best practice is to use the formula Math.floor(date.getMonth() / 3) + 1.
  • This works because date.getMonth() is zero-based, and dividing by 3 groups the months into the correct quarter.
  • Encapsulating this logic in a reusable getQuarter(date) function is a great way to keep your code clean and maintainable.