In Moment.js, how do you get the current financial

2020-02-23 06:24发布

问题:

Is there a simple/built in way of figuring out the current financial quarter?

ex:

  • Jan-Mar: 1st
  • Apr-Jul: 2nd
  • Jul-Sept: 3rd
  • Oct-Dec: 4th

回答1:

This is now supported in moment:

moment('2014-12-01').utc().quarter() //outputs 4
moment().quarter(); //outputs current quarter ie. 2

Documentation



回答2:

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};
}


回答3:

Use this simple code to get all quarter based on january and april

Demo

Code :

 // startMonth should be january or april

  function setQuarter(startMonth) {
    var obj = {};
    if(startMonth=='january'){

        obj.quarter1 = {start:moment().month(0).startOf('month'),end:moment().month(2).endOf('month')}
        obj.quarter2 = {start:moment().month(3).startOf('month'),end:moment().month(5).endOf('month')}
        obj.quarter3 = {start:moment().month(6).startOf('month'),end:moment().month(8).endOf('month')}
        obj.quarter4 = {start:moment().month(9).startOf('month'),end:moment().month(11).endOf('month')}
        console.log(obj);
        return obj;
    }
    else if(startMonth=='april'){

        obj.quarter1 = {start:moment().month(3).startOf('month'),end:moment().month(5).endOf('month')}
        obj.quarter2 = {start:moment().month(6).startOf('month'),end:moment().month(8).endOf('month')}
        obj.quarter3 = {start:moment().month(9).startOf('month'),end:moment().month(11).endOf('month')}
        obj.quarter4 = {start:moment().month(0).startOf('month').add('years',1),end:moment().month(2).endOf('month').add('years',1)}
        console.log(obj);
        return obj;
    }
}

 setQuarter('april');

Fiddle



回答4:

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.



回答5:

There is nothing built in right now, but there is conversation to add formatting tokens for quarters. https://github.com/timrwood/moment/pull/540

In the meantime, you could use something like the following.

Math.floor(moment().month() / 3) + 1;

Or, if you want it on the moment prototype, do this.

moment.fn.quarter = function () {
    return Math.floor(this.month() / 3) + 1;
}


回答6:

The formula that seems to work for me is:

Math.ceil((moment().month() + 1) / 3);

moment().month() gives back the 0-11 month format so we have to add one

THE ACTUAL MONTH = (moment().month() + 1)

then we have to divide by 3 since there are 3 months in a quarter.

HOW MANY QUARTERS PASSED = (THE ACTUAL MONTH) / 3

and then we have to get the ceiling of that (round to the nearest quarter end)

CEILING(HOW MANY QUARTERS PASSED)

EDIT:

The Official formula (not commited yet) is:

~~((this.month()) / 3) + 1;

which means Math.floor((this.month()) / 3) + 1;



回答7:

The simplist way to do this is

Math.floor(moment.month() / 3)

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.



回答8:

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

obj.quarter1.start date returns, 1-April-CurrentYear [incorrect Value]
obj.quarter4.end date retunrs, 31-March-NextYear [incorrect Value]

Correct values should be,

Start = 1-April-PreviuosYear
End = 31-March-CurrentYear

So, Taking consideration for first 3 month it can be written something like,

    const obj = {};

/* 0-Jan, 1-Feb, 2-Mar */
    if (moment().month() <= 2) { 
        obj.quarter1 = { start: moment().month(3).startOf('month').add('years', -1), end: moment().month(5).endOf('month').add('years', -1) };
        obj.quarter2 = { start: moment().month(6).startOf('month').add('years', -1), end: moment().month(8).endOf('month').add('years', -1) };
        obj.quarter3 = { start: moment().month(9).startOf('month').add('years', -1), end: moment().month(11).endOf('month').add('years', -1) };
        obj.quarter4 = { start: moment().month(0).startOf('month'), end: moment().month(2).endOf('month') };    
    } else {
        obj.quarter1 = { start: moment().month(3).startOf('month'), end: moment().month(5).endOf('month') };
        obj.quarter2 = { start: moment().month(6).startOf('month'), end: moment().month(8).endOf('month') };
        obj.quarter3 = { start: moment().month(9).startOf('month'), end: moment().month(11).endOf('month') };
        obj.quarter4 = { start: moment().month(0).startOf('month').add('years', 1), end: moment().month(2).endOf('month').add('years', 1) };    
    }
    console.log(obj);