How to get the Quarter from a Date in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 5, 2024
3 min

banner

# Table of Contents

  1. Get the Quarter from a Date using JavaScript
  2. Get the number of days left in a Quarter

# Get the Quarter from a Date using JavaScript

To get the quarter from a date:

  1. Use the getMonth() method to get a zero-based value for the month of the given date.
  2. Divide the number of the month by 3.
  3. Add 1 to the result.
  4. Pass the final value to the Math.floor() function.
index.js
/** * January 1st - March 31st = First Quarter * April 1st - June 30th = Second Quarter * July 1st - September 30th = Third Quarter * October 1st - December 31st = Fourth Quarter */ function getQuarter(date = new Date()) { return Math.floor(date.getMonth() / 3 + 1); } console.log(getQuarter()); // ๐Ÿ‘‰๏ธ current quarter โœ… console.log(getQuarter(new Date('2023-01-24'))); // ๐Ÿ‘‰๏ธ 1 console.log(getQuarter(new Date('2023-02-24'))); // ๐Ÿ‘‰๏ธ 1 console.log(getQuarter(new Date('2023-03-24'))); // ๐Ÿ‘‰๏ธ 1 console.log(getQuarter(new Date('2023-04-24'))); // ๐Ÿ‘‰๏ธ 2 console.log(getQuarter(new Date('2023-05-24'))); // ๐Ÿ‘‰๏ธ 2 console.log(getQuarter(new Date('2023-06-24'))); // ๐Ÿ‘‰๏ธ 2 console.log(getQuarter(new Date('2023-07-24'))); // ๐Ÿ‘‰๏ธ 3 console.log(getQuarter(new Date('2023-08-24'))); // ๐Ÿ‘‰๏ธ 3 console.log(getQuarter(new Date('2023-09-24'))); // ๐Ÿ‘‰๏ธ 3 console.log(getQuarter(new Date('2023-10-24'))); // ๐Ÿ‘‰๏ธ 4 console.log(getQuarter(new Date('2023-11-24'))); // ๐Ÿ‘‰๏ธ 4 console.log(getQuarter(new Date('2023-12-24'))); // ๐Ÿ‘‰๏ธ 4

get quarter from date

The code for this article is available on GitHub

We created a reusable function that gets the quarter from a date.

If a date is not provided to the function, it returns the quarter from the current date.

We used the Date.getMonth() method to get a zero-based value of the month of the date.

The method returns an integer between 0 and 11, where 0 corresponds to January, 1 to February, etc.

We divided the number by 3 and had to add 1 to the result because of how the getMonth method works.

Finally, we passed the value to the Math.floor() function, which returns the largest integer less than or equal to a given number. In other words, it rounds a number down if it has a decimal.

The Date() constructor returns a Date object. You should either pass a Date object as a parameter to the getQuarter function or nothing at all.

Notice that we provided a default value for the date parameter. If a date is not passed to the getQuarter function it returns a number that represents the current quarter.

The same approach can be used if your country has different start/end dates for the quarters.

# Get the number of days left in a Quarter

To get the number of days left in a quarter:

  1. Get the start date of the next quarter.
  2. Find the difference in days between the provided date and the date of the next quarter.
index.js
function getQuarter(date = new Date()) { return Math.floor(date.getMonth() / 3 + 1); } function daysLeftInQuarter(date = new Date()) { const quarter = getQuarter(date); const nextQuarter = new Date(); if (quarter === 4) { nextQuarter.setFullYear(date.getFullYear() + 1, 0, 1); } else { nextQuarter.setFullYear(date.getFullYear(), quarter * 3, 1); } const ms1 = date.getTime(); const ms2 = nextQuarter.getTime(); return Math.floor((ms2 - ms1) / (24 * 60 * 60 * 1000)); } console.log(daysLeftInQuarter(new Date())); // ๐Ÿ‘‰๏ธ current date til next quarter console.log(daysLeftInQuarter(new Date('2022-03-01'))); // ๐Ÿ‘‰๏ธ 31 console.log(daysLeftInQuarter(new Date('2022-03-29'))); // ๐Ÿ‘‰๏ธ 3 console.log(daysLeftInQuarter(new Date('2022-12-30'))); // ๐Ÿ‘‰๏ธ 2 console.log(daysLeftInQuarter(new Date('2022-12-31'))); // ๐Ÿ‘‰๏ธ 1 console.log(daysLeftInQuarter(new Date('2022-03-31'))); // ๐Ÿ‘‰๏ธ 1

get number of days left in quarter

The code for this article is available on GitHub

We created a reusable function that calculates how many days are left in a quarter.

The setFullYear() method sets the full year for a specified date.

The method takes 3 parameters:

  • yearValue - an integer specifying the year, e.g. 2023
  • monthValue (optional) - an integer between 0 (January) and 11 (December) representing the month
  • dateValue (optional) - an integer between 1 and 31 representing the day of the month
If the quarter is the last, we know that we have to adjust the year as well.

We calculated the starting date of the next quarter and saved it in the nextQuarter variable.

We used the getTime() method to get the number of milliseconds since the Unix Epoch, for the passed-in date and for the start date of the next quarter.

Finally, we subtracted the milliseconds of the provided date from the milliseconds of the start date of the next quarter and converted the result to days.

Again, we used the Math.floor() function to round the result down if it has a decimal. Depending on your use case, you might not need to do that.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev