In Moment.js, how do you get the current financial Quarter?
This is now supported in moment:
moment('2014-12-01').utc().quarter() //outputs 4
moment().quarter(); //outputs current quarter ie. 2
Documentation
Using version 2.14.1+ you can do something like the following:
moment().quarter()
returns the current quarter number: 1, 2, 3, 4.
moment().quarter(moment().quarter()).startOf('quarter');
Would return the current quarter with the date set to the quarter starting date.
moment().quarter(moment().quarter()).startOf('quarter');
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};
}