I dont think any of these answers explain how to get the financial quarter. They explain how to get the calendar quarter.
I do not have a clean answer as thats what led me here. But the fiscal quarter is what is really wanted. And that is based on the start month of the fiscal year.
For example if my company's fiscal start month is February. Then at the time of writing this January 9th 2017 I'm actually in Q4 2016.
To accomplish this we need a way to get the quarter relative to a supplied integer of the start month.
Would return the current quarter with the date set to quarter ending date.
You could also define a function that takes the corresponding quarter number as argument (1,2,3,4), and returns an object containing the start and end date of the quarter.
function getQuarterRange(quarter) {
const start = moment().quarter(quarter).startOf('quarter');
const end = moment().quarter(quarter).endOf('quarter');
return {start, end};
}
Answer given by Nishchit Dhanani, is correct but has one issue in 'April' scenario.
Issue: If your financial year is April than, For first 3 months i.e. JAN, FEB & MAR
Correct values should be,
So, Taking consideration for first 3 month it can be written something like,
I dont think any of these answers explain how to get the financial quarter. They explain how to get the calendar quarter.
I do not have a clean answer as thats what led me here. But the fiscal quarter is what is really wanted. And that is based on the start month of the fiscal year.
For example if my company's fiscal start month is February. Then at the time of writing this January 9th 2017 I'm actually in Q4 2016.
To accomplish this we need a way to get the quarter relative to a supplied integer of the start month.
This is now supported in moment:
Documentation
Use this simple code to get all quarter based on january and april
Demo
Code :
Fiddle
The simplist way to do this is
That will give you the zero based quarter index. ie 0, 1, 2, or 3.
Then, if you want the quarter's literal number, just add one.
Using version 2.14.1+ you can do something like the following:
moment().quarter()
returns the current quarter number: 1, 2, 3, 4.Would return the current quarter with the date set to the quarter starting date.
Would return the current quarter with the date set to quarter ending date.
You could also define a function that takes the corresponding quarter number as argument (1,2,3,4), and returns an object containing the start and end date of the quarter.