gpt4 book ai didi

javascript - 在 JavaScript 中获取最后 n 个季度

转载 作者:行者123 更新时间:2023-11-30 12:08:01 25 4
gpt4 key购买 nike

显然,this kind of question was asked before ,但这是一个我无法翻译成 JavaScript 的 C# 答案。

这是我的原始代码,但现在第一季度失败了:

var amount = 6;
var now = new Date();
var thisYear = now.getFullYear();
var quarterStart = Math.ceil((now.getMonth() + 1) / 3);
//quarterStart will return a number between 1 and 4

for (var i = 0; i < amount; i++) {

//here's the line that definitely needs to be changed
var thisQuarter = quarterStart - (i % 4);

//if thisQuarter is the last one, go back one year
if (thisQuarter == 4 && i > 0) {
thisYear--;
};

console.log('Q' + thisQuarter + ' ' + thisYear);
};

//console should return the following
//'Q1 2016'
//'Q4 2015'
//'Q3 2015'
//'Q2 2015'
//'Q1 2015'
//'Q4 2014'

编辑:只要输出如图所示,就可以更改循环的设置。

最佳答案

我对您的代码做了一些更改,现在应该可以工作了。

var amount = 6;
var now = new Date();
var thisYear = now.getFullYear();
var quarterStart = Math.ceil((now.getMonth() + 1) / 3);

for (var i = 0; i < amount; i++) {
console.log('Q' + quarterStart + ' ' + thisYear);

quarterStart--;
if (quarterStart <= 0) {
thisYear--;
quarterStart = 4;
};
};

关于javascript - 在 JavaScript 中获取最后 n 个季度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34624323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com