To get the current quarter start and end date in JavaScript,
- Get the current date object by calling the
Date()
constructor without any parameters. - Get the current month of the year by calling the
getMonth()
method on the current Date object. - Calculate the current quarter by dividing the current month by 3.
- Get the current year from the current date object by calling the
getFullYear()
method on it. - Pass the year, month and day to the
Date()
constructor to get the start and end date of the quarter
For example,
// Get Today's date(March 22, 2023) const today = new Date(); // Get current quarter const currentQuarter = Math.floor((today.getMonth() / 3)); // Get current year const year = today.getFullYear(); // Get current quarter's start and end date let quarterStart = new Date(year, currentQuarter * 3, 1); let quarterEnd = new Date(year, (currentQuarter + 1) * 3, 0); console.log('Quarter start date: ', quarterStart); console.log('Quarter end date: ', quarterEnd);
Output:
Quarter start date: Sun Jan 01 2023 00:00:00 GMT+0530 (India Standard Time) Quarter end date: Fri Mar 31 2023 00:00:00 GMT+0530 (India Standard Time)
In this example, we have used two built-in date methods getMonth()
and getFullYear()
.
The getMonth()
method returns an integer between 0 and 11 representing the month of the year. 0 corresponds to January, 1 corresponds to February and so on.
When you call the getMonth()
method on the current date object, it gives you the index(0-based) of the current month of the year.
For example,
// Today's date(March 22, 2023) const date = new Date(); console.log(date.getMonth()); // Output: 👉 2
The getFullYear()
method on the other hand returns an integer representing the year for the specified date.
When you call the getFullYear()
method on the current date object, it gives you the current year.
For example,
// Today's date(March 22, 2023) const date = new Date(); console.log(date.getFullYear()); // Output: 👉 2023
Now, if we have the value of the year and month, we can easily get the first day’s date of that month by passing the year, month and day parameters to the Date()
constructor.
Since we want the first day’s date, therefore, we have to pass the day as 1 to the Date()
constructor.
For example,
// Get first day's date of March, 2023 const date = new Date(2023, 2, 1); console.log(date); // Output: 👉 Wed Mar 01 2023 00:00:00 GMT+0530 (India Standard Time)
Please note that the month is 0-based, therefore, we have passed the month index 2, not 3 to get the first day’s date of March.
Getting the last day of the month is a little bit trickier.
To get the last day’s date of a month, you have to first go to the next month and then pass the day parameter as 0 to the Date()
constructor.
For example, if we want to get the last day’s date of March month, we have to pass the month parameter as 3(2 + 1) and the day parameter as 0 to get one day back from April month.
// Get last day's date of March, 2023 const date = new Date(2023, 2 + 1, 0); console.log(date); // Output: 👉 Fri Mar 31 2023 00:00:00 GMT+0530 (India Standard Time)
Get the Quarter Start and End Date for a Given Date
If you already have a date string and want to get the quarter’s start and end date for that date, you can follow the same steps discussed in the previous example, except that you have to pass the date string to the Date()
constructor as an argument to get the date object corresponding to that date string.
For example,
// Given date(April 10, 2023) const date = new Date('2023-04-10'); // Get quarter const quarter = Math.floor((date.getMonth() / 3)); // Get year from the date const year = date.getFullYear(); // Get quarter's start and end date let quarterStart = new Date(year, quarter * 3, 1); let quarterEnd = new Date(year, (quarter + 1) * 3, 0); console.log('Quarter start date: ', quarterStart); console.log('Quarter end date: ', quarterEnd);
Output:
Quarter start date: Sat Apr 01 2023 00:00:00 GMT+0530 (India Standard Time) Quarter end date: Fri Jun 30 2023 00:00:00 GMT+0530 (India Standard Time)
That’s all for this article. Thanks for reading!