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:56

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;
}
查看更多
聊天终结者
3楼-- · 2020-02-23 07:00

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;

查看更多
登录 后发表回答