Javascript 在 Moment.js 中,如何获取当前的财务季度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14046679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Moment.js, how do you get the current financial Quarter?
提问by Toli
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 月至 3 月:1 日
- 4-7月:2日
- 7 月至 9 月:3 日
- 10 月至 12 月:4 日
回答by brian2013
This is now supported in moment:
现在支持这一点:
moment('2014-12-01').utc().quarter() //outputs 4
moment().quarter(); //outputs current quarter ie. 2
回答by Andres Zapata
Using version 2.14.1+ you can do something like the following:
使用 2.14.1+ 版本,您可以执行以下操作:
moment().quarter()returns the current quarter number: 1, 2, 3, 4.
moment().quarter()返回当前季度编号: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.
您还可以定义一个函数,该函数将相应的季度编号作为参数 (1,2,3,4),并返回一个包含该季度开始和结束日期的对象。
function getQuarterRange(quarter) {
const start = moment().quarter(quarter).startOf('quarter');
const end = moment().quarter(quarter).endOf('quarter');
return {start, end};
}
回答by Nishchit Dhanani
Use this simple code to get all quarter based on january and april
使用这个简单的代码获得基于 1 月和 4 月的所有季度
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');
回答by user2985295
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.
例如,如果我公司的会计开始月份是 2 月。然后在 2017 年 1 月 9 日撰写本文时,我实际上是在 2016 年第四季度。
To accomplish this we need a way to get the quarter relative to a supplied integer of the start month.
为了实现这一点,我们需要一种方法来获取相对于提供的起始月份整数的季度。
回答by timrwood
There is nothing built in right now, but there is conversation to add formatting tokens for quarters. https://github.com/timrwood/moment/pull/540
现在没有内置任何东西,但是有对话可以为宿舍添加格式化标记。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;
}
回答by Toli
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
moment().month() 返回 0-11 个月的格式,因此我们必须添加一个
THE ACTUAL MONTH = (moment().month() + 1)
then we have to divide by 3 since there are 3 months in a quarter.
那么我们必须除以 3,因为一个季度有 3 个月。
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:
该官方式(尚未COMMITED)为:
~~((this.month()) / 3) + 1;
which means Math.floor((this.month()) / 3) + 1;
意思是 Math.floor((this.month()) / 3) + 1;
回答by thomallen
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.
这将为您提供基于零的季度指数。即 0、1、2 或 3。
Then, if you want the quarter's literal number, just add one.
然后,如果您想要季度的字面数字,只需添加一个。
回答by JiteshW
Answer given by Nishchit Dhanani, is correct but has one issue in 'April' scenario.
Nishchit Dhanani给出的答案是正确的,但在“四月”场景中有一个问题。
Issue: If your financial year is April than, For first 3 months i.e. JAN, FEB & MAR
问题:如果您的财政年度是 4 月,则前 3 个月即 1 月、2 月和 3 月
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,
所以,考虑到前 3 个月,它可以写成这样,
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);

