是否有简单/内置的方法来计算当前的财务季度?
例如:
答案 0 :(得分:38)
现在支持这一点。很简单:
moment('2014-12-01').utc().quarter() //outputs 4
moment().quarter(); //outputs current quarter ie. 2
答案 1 :(得分:12)
使用版本2.14.1+,您可以执行以下操作:
moment().quarter()
返回当前的季度编号:1,2,3,4。
moment().quarter(moment().quarter()).startOf('quarter');
将返回当前季度,并将日期设置为季度开始日期。
moment().quarter(moment().quarter()).startOf('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};
}
答案 2 :(得分:4)
使用这个简单的代码来获取基于1月和4月的所有季度
代码:
// 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');
答案 3 :(得分:3)
我认为这些答案中的任何一个都不能解释如何获得财务季度。他们解释了如何获得日历季度。
我没有一个干净的答案,因为这就是我在这里的原因。但财政季度才是真正想要的。这是基于财政年度的开始月份。
例如,如果我公司的财政开始月份是2月份。然后在2017年1月9日撰写本文时,我实际上是在2016年第4季度。
要实现这一点,我们需要一种方法来获得相对于开始月份的提供整数的季度。
答案 4 :(得分:2)
开始日期
react-router-dom 4.3.1
将返回当前季度并将日期设置为季度开始日期。
import 'dart:convert';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: HomePage());
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _chosenValue;
void _showDecline() {
showDialog(
context: context,
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return AlertDialog(
title: new Text("Decline Appointment Request"),
content:
Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
new Text("Please select an option for why you declined."),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: new DropdownButton<String>(
hint: Text('Select one option'),
value: _chosenValue,
underline: Container(),
items: <String>[
'I\'m not able to help',
'Unclear description',
'Not available at set date and time',
'Other'
].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(
value,
style: TextStyle(fontWeight: FontWeight.w500),
),
);
}).toList(),
onChanged: (String value) {
setState(() {
_chosenValue = value;
});
},
)),
]),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
child: FlatButton(child: Text('Click'), onPressed: _showDecline),
),
),
);
}
}
将返回“ 2019”年第四季度的开始日期。
moment().quarter(moment().quarter()).startOf('quarter');
将返回当年当前季度的开始日期。
结束日期
moment("2019", "YYYY").quarter(4).startOf('quarter');
将返回当前季度并将日期设置为季度结束日期。
moment().startOf('quarter');
将返回“ 2019”年第四季度的结束日期。
moment().quarter(moment().quarter()).endOf('quarter');
将返回当年当前季度的结束日期。
答案 5 :(得分:1)
现在没有任何内置功能,但有会话可以为季度添加格式化令牌。 https://github.com/timrwood/moment/pull/540
与此同时,您可以使用以下内容。
Math.floor(moment().month() / 3) + 1;
或者,如果你想要它的原型,请执行此操作。
moment.fn.quarter = function () {
return Math.floor(this.month() / 3) + 1;
}
答案 6 :(得分:1)
似乎对我有用的公式是:
Math.ceil((moment().month() + 1) / 3);
moment()。month()返回0-11个月的格式,因此我们必须添加一个
THE ACTUAL MONTH = (moment().month() + 1)
然后我们必须除以3,因为一个季度有3个月。
HOW MANY QUARTERS PASSED = (THE ACTUAL MONTH) / 3
然后我们必须得到它的上限(舍入到最近的季度末)
CEILING(HOW MANY QUARTERS PASSED)
编辑:
Official formula(尚未提交)是:
~~((this.month()) / 3) + 1;
表示Math.floor((this.month()) / 3) + 1;
答案 7 :(得分:0)
最简单的方法是
Math.floor(moment.month() / 3)
这将为您提供基于零的四分之一指数。即0,1,2或3.
然后,如果你想要季度的文字数字,只需加一个。
答案 8 :(得分:0)
Nishchit Dhanani给出的答案是正确的,但在“四月”场景中存在一个问题。
问题:如果您的财政年度为4月,则为前3个月,即JAN,FEB和MAR
obj.quarter1.start date returns, 1-April-CurrentYear [incorrect Value]
obj.quarter4.end date retunrs, 31-March-NextYear [incorrect Value]
正确的值应该是
Start = 1-April-PreviuosYear
End = 31-March-CurrentYear
因此,考虑到前三个月,可以这样写,
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);