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

后端 未结 9 1680
深忆病人
深忆病人 2020-12-14 16:07

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

ex:

  • Jan-Mar: 1st
  • Apr-Jul: 2nd
  • Jul-Sept: 3rd
相关标签:
9条回答
  • 2020-12-14 16:39

    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);
    
    0 讨论(0)
  • 2020-12-14 16:43

    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

    0 讨论(0)
  • 2020-12-14 16:49

    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;

    0 讨论(0)
  • 2020-12-14 16:51

    This is now supported in moment:

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

    Documentation

    0 讨论(0)
  • 2020-12-14 16:51

    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};
    }
    
    0 讨论(0)
  • 2020-12-14 16:52

    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.

    0 讨论(0)
提交回复
热议问题