使用 javascript 获取年度当前季度
Get current quarter in year with javascript
我怎样才能用javascript获得当前季度?我正在尝试检测我们目前处于哪个季度,例如 2。
编辑 我如何计算该季度剩余的天数?
假设 1 月至 3 月被视为第 1 季度(某些国家/公司将其财政年度与日历年分开),则以下代码应有效:
var today = new Date();
var quarter = Math.floor((today.getMonth() + 3) / 3);
这为您提供:
Month getMonth() quarter
--------- ---------- -------
January 0 1
February 1 1
March 2 1
April 3 2
May 4 2
June 5 2
July 6 3
August 7 3
September 8 3
October 9 4
November 10 4
December 11 4
至于如何获得该季度的剩余天数,基本上是弄清楚下个季度的第一天并计算出差异,例如:
var today = new Date();
var quarter = Math.floor((today.getMonth() + 3) / 3);
var nextq;
if (quarter == 4) {
nextq = new Date (today.getFullYear() + 1, 1, 1);
} else {
nextq = new Date (today.getFullYear(), quarter * 3, 1);
}
var millis1 = today.getTime();
var millis2 = nextq.getTime();
var daydiff = (millis2 - millis1) / 1000 / 60 / 60 / 24;
这是未经检验的,但理论是合理的。基本上创建一个对应于下一个季度的日期,将其和今天转换为自纪元开始以来的毫秒,那么差异就是毫秒数。
将其除以一天中的毫秒数,您就有了以天为单位的差异。
这给了你(至少大致)本季度剩余的天数。您可能需要对其进行微调,以确保所有时间都设置为相同的值 (00:00:00),以便差异以确切的天数为单位。
它也可能会相差一,具体取决于您对"季度剩余天数"的实际定义。
但这应该是一个很好的起点。
鉴于您没有提供任何标准来确定"*我们目前处于哪个季度",可以建议一种算法,然后您必须适应您需要的任何标准。
例如// For the US Government fiscal year
// Oct-Dec = 1
// Jan-Mar = 2
// Apr-Jun = 3
// Jul-Sep = 4
function getQuarter(d) {
d = d || new Date();
var m = Math.floor(d.getMonth()/3) + 2;
return m > 4? m - 4 : m;
}
作为可运行的代码段,包括年份:
function getQuarter(d) {
d = d || new Date();
var m = Math.floor(d.getMonth() / 3) + 2;
m -= m > 4 ? 4 : 0;
var y = d.getFullYear() + (m == 1? 1 : 0);
return [y,m];
}
console.log(`The current US fiscal quarter is ${getQuarter().join('Q')}`);
console.log(`1 July 2018 is ${getQuarter(new Date(2018,6,1)).join('Q')}`);
然后,您可以根据需要将其调整为各个财务或日历季度。您还可以执行以下操作:
function getQuarter(d) {
d = d || new Date(); // If no date supplied, use today
var q = [4,1,2,3];
return q[Math.floor(d.getMonth() / 3)];
}
然后根据所需季度的定义使用不同的q
数组。
编辑
如果它们从 1 月 1 日、4 月、7 月和 10 月开始,下面列出了一个季度的剩余天数,它在各种浏览器中进行了测试,包括 IE 6(尽管因为它使用基本的 ECMAScript,它应该可以在任何地方工作):
function daysLeftInQuarter(d) {
d = d || new Date();
var qEnd = new Date(d);
qEnd.setMonth(qEnd.getMonth() + 3 - qEnd.getMonth() % 3, 0);
return Math.floor((qEnd - d) / 8.64e7);
}
function getQuarter(d) {
return Math.floor(d.getMonth()/3) + 1);
}
编辑:我省略了%4,完全正确,谢谢克莱门特
如果第一个解决方案不起作用,您可以将其调整到您想要的范围
var today = new Date();
var month = now.getMonth();
var quarter;
if (month < 4)
quarter = 1;
else if (month < 7)
quarter = 2;
else if (month < 10)
quarter = 3;
else if (month < 13)
quarter = 4;
您可以使用时刻包:
使用矩包回答您的问题是:
moment().quarter()
以下是使用时刻套餐的一个季度的开始和结束日期:
季度开始日期
moment().quarter(moment().quarter()).startOf('quarter');
将返回当前季度,并将日期设置为季度开始日期。
moment("2019", "YYYY").quarter(4).startOf('quarter');
将返回今年第四季度的开始日期"2019"。
moment().startOf('quarter');
将返回当前年度当前季度的开始日期。
季度结束日期
moment().quarter(moment().quarter()).endOf('quarter');
将返回当前季度,并将日期设置为季度结束日期。
moment("2019", "YYYY").quarter(4).endOf('quarter');
将返回今年第四季度的结束日期"2019"。
moment().endOf('quarter');
将返回当前年度当前季度的结束日期。
取决于月份
var date = new Date();
var quarter = parseInt(date.getMonth() / 3 ) + 1 ;
取决于日期
var date = new Date();
var firstday = new Date(date.getFullYear(),0,1); // XXXX/01/01
var diff = Math.ceil((date - firstday) / 86400000);
// a quarter is about 365/4
quarter = parseInt( diff / ( 365/ 4 )) + 1
// if today is 2012/01/01, the value of quarter is 1.
我有用!
var d = new Date();
var quarter = Math.ceil(d.getMonth() / 3);
console.log(quarter)
通用,快速性能(无除法,无浮点数),易于修改为其他标准
const now = new Date()
const currentMonth = now.getMonth() + 1
const currentQuarter = {
1: 1, // January
2: 1, // February
3: 1, // March
4: 2, // April
5: 2, // May
6: 2, // June
7: 3, // July
8: 3, // August
9: 3, // September
10: 4, // October
11: 4, // November
12: 4, // December
}[currentMonth]
console.log(currentQuarter)
为什么你们都使用地板,而你可以简单地使用ceil
.
function getQuarter(m) {
return Math.ceil(m / 3)
}
// Test it
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].map(getQuarter);
如果月份计数从 0 开始,只需将 1 添加到月份数:
function getQuarter(date) {
const m = date.getMonth() + 1;
return Math.ceil(m / 3)
}
它既不高效也不可读,但它具有单行风格。
(new Date(new Date().getFullYear(), Math.floor((new Date().getMonth() + 3) / 3) * 3, 1) - new Date()) / 86400000
// Set Period Function
SetPeriod(SelectedVal) {
try {
if (SelectedVal === '0') { return; }
if (SelectedVal != null) {
let yrf: number, mtf: number, dyf: number, yrt: number, mtt: number, dyt: number, dtf: any, dtt: any;
let dat = new Date();
let q = 0;
switch (SelectedVal) {
case '-1': // Not specify
frm = ''; to = '';
return;
case '0': // As specify
break;
case '1': // This Month
yrf = yrt = dat.getUTCFullYear();
mtf = mtt = dat.getUTCMonth();
dyf = 1; dyt = this.getDaysInMonth(mtf, yrf);
break;
case '2': // Last Month
dat.setDate(0); // 0 will result in the last day of the previous month
dat.setDate(1); // 1 will result in the first day of the month
yrf = yrt = dat.getUTCFullYear();
mtf = mtt = dat.getUTCMonth();
dyf = 1; dyt = this.getDaysInMonth(mtf, yrf);
break;
case '3': // This quater
q = Math.ceil((dat.getUTCMonth()) / 3);
// tslint:disable-next-line:no-switch-case-fall-through
case '4': // Last quater
if (q === 0) { q = Math.ceil(dat.getUTCMonth() / 3) - 1; if (q === 0) { q = 1; } }
yrf = yrt = dat.getUTCFullYear();
if (q === 1) {
mtf = 0; mtt = 2;
dyf = 1; dyt = 31;
} else if (q === 2) {
mtf = 3; mtt = 5;
dyf = 1; dyt = 30;
} else if (q === 3) {
mtf = 6; mtt = 8;
dyf = 1; dyt = 30;
} else if (q === 4) {
mtf = 9; mtt = 11;
dyf = 1; dyt = 31;
}
break;
case '6': // Last year
dat = new Date(dat.getUTCFullYear(), 0, 1);
// tslint:disable-next-line:no-switch-case-fall-through
case '5': // This year
yrf = yrt = dat.getUTCFullYear();
mtf = 0; mtt = 11;
dyf = 1; dyt = 31;
break;
}
// Convert to new Date
dtf = new Date(yrf, mtf, dyf);
dtt = new Date(yrt, mtt, dyt);
console.log('dtf', dtf);
console.log('dtt', dtt);
}
} catch (e) {
alert(e);
}
}
// Get Day in Month
getDaysInMonth = (month: number, year: number) => {
return new Date(year, month + 1, 0).getDate();
}
就我而言,季度取决于输入季度开始日期(月、日)和季度持续时间(3、6)
function getCurrentQuarter(startDateString, quarterDuration) {
var startDate = new Date(startDateString)
// default order of months
var months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
let startMonth = startDate.getMonth() + 1
let startMonthIndex = months.indexOf(startMonth)
// rotate months to get start month first
if (startMonthIndex != 0) {
var monthsNew = months.slice(startMonthIndex, months.length)
months = monthsNew.concat(months.slice(0, startMonthIndex))
}
let today = new Date()
let thisMonth = today.getMonth() + 1
let thisMonthIndex = months.indexOf(thisMonth) + 1
// compare with date
let thisDay = today.getDate()
let startDay = startDate.getDate()
if (thisDay < startDay) {
// this is previous month
if (thisMonthIndex == 1) {
thisMonthIndex = 12
} else {
thisMonthIndex = thisMonthIndex - 1
}
}
// find quarter
let thisQuarter = Math.ceil(thisMonthIndex / quarterDuration)
return thisQuarter
}
获取当前季度('2021-03-15', 3)
对于任何关心财政季度的人来说,这里有一个需要0(1)时间的简单方法。 首先,既然这是基于月份的固定数据,为什么不直接创建一个关联数组呢? 请记住,JavaScript 月份从 0 开始,到 11。
** getMonth(): Fiscal quarter
const FISCAL_QUARTER = {
0:2,1:2,2:2,3:3,4:3,5:3,6:4,7:4,8:4,9:1,10:1,11:1
};
function getFiscalQuarter(d){
return FISCAL_QUARTER[d.getMonth()];
}
console.log(getFiscalQuarter(new Date()));
的看法
var todayDate = new Date(new Date().setUTCHours(0, 0, 0, 0));
var currentQuarter = Math.floor(todayDate.getMonth() / 3);
var firstDate = new Date(
new Date(todayDate.getFullYear(), currentQuarter * 3, 1).setUTCHours(0, 0, 0, 0)
);
var endDate = new Date(
new Date(
firstDate.getFullYear(),
firstDate.getMonth() + 3,
0
).setUTCHours(0, 0, 0, 0)
);
console.log(endDate)
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 使用php或javascript从facebook相册URL中删除多余的部分
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在JavaScript中将字符串转换为函数引用
- 模糊事件的Javascript测试
- Javascript更改图标
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何使用WCF服务和javascript表单post上传.doc文件
- javascript结合了数组和字典
- 这是什么 ==- javascript 运算符
- 从javascript创建一个列表
- 无法在通过jQuery的ajax加载的页面中执行javascript
- 使用 javascript 获取年度当前季度
- 用JavaScript获取最后n个季度
- 通过Javascript获取上一季度和本季度的日期
- 年度季度PHP/Javascript模式
- JavaScript 将时间返回到最接近的季度
- 前几个季度的javascript