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

2020-02-23 06:17发布

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

8条回答
该账号已被封号
2楼-- · 2020-02-23 06:44

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);
查看更多
Luminary・发光体
3楼-- · 2020-02-23 06:45

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.

查看更多
够拽才男人
4楼-- · 2020-02-23 06:48

This is now supported in moment:

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

Documentation

查看更多
男人必须洒脱
5楼-- · 2020-02-23 06:49

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

查看更多
虎瘦雄心在
6楼-- · 2020-02-23 06:52

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.

查看更多
Viruses.
7楼-- · 2020-02-23 06:55

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};
}
查看更多
登录 后发表回答