gpt4 book ai didi

javascript - 在 Javascript 中获取下一个 12 个月的结果搞砸了

转载 作者:搜寻专家 更新时间:2023-11-01 04:46:06 25 4
gpt4 key购买 nike

我有以下代码用于生成从今天开始的 future 12 个月(含)的列表:

function DateUtilFunctions() {
var self = this;

var monthNames = new Array();

monthNames[0] = "January";
monthNames[1] = "February";
monthNames[2] = "March";
monthNames[3] = "April";
monthNames[4] = "May";
monthNames[5] = "June";
monthNames[6] = "July";
monthNames[7] = "August";
monthNames[8] = "September";
monthNames[9] = "October";
monthNames[10] = "November";
monthNames[11] = "December";

self.getNext12MonthNamesWithYear = function () {
var months = new Array();
var today = new Date(Date());

var loopDate = new Date();
loopDate.setTime(today.valueOf());

var todayPlus12Months = new Date(today.setMonth(today.getMonth() + 12));

while (loopDate.valueOf() < todayPlus12Months.valueOf()) {
alert(loopDate);
alert(loopDate.getMonth());
var month = monthNames[loopDate.getMonth()];


months.push(month + ' ' + loopDate.getFullYear());
loopDate.setMonth(loopDate.getMonth() + 1);
}

return months;
};
}

调用 getNext12MonthNamesWithYear() 的结果是:

  • “2012 年 5 月”
  • “2012 年 7 月”
  • “2012 年 8 月”
  • “2012 年 5 月”
  • “2012 年 7 月”
  • “2012 年 8 月”
  • “2012 年 9 月”
  • “2012 年 10 月”
  • “2012 年 11 月”
  • “2012 年 12 月”
  • “2013 年 1 月”
  • “2013 年 2 月”
  • “2013 年 3 月”
  • “2013 年 4 月”
  • “2013 年 5 月”

如您所见,列表的开头有点奇怪,因为缺少“June”,加上“May”、“July”和“August”出现了两次。

当然,我在这里做错了;有人可以帮帮我吗?

编辑:

根据 micadelli 的评论,这是我使用的解决方案:

function DateUtilFunctions() {
var self = this;

var monthNames = new Array();

monthNames[0] = "January";
monthNames[1] = "February";
monthNames[2] = "March";
monthNames[3] = "April";
monthNames[4] = "May";
monthNames[5] = "June";
monthNames[6] = "July";
monthNames[7] = "August";
monthNames[8] = "September";
monthNames[9] = "October";
monthNames[10] = "November";
monthNames[11] = "December";

self.getNext12MonthNamesWithYear = function () {
var months = new Array();
var today = new Date();
var tmpDate = new Date();
var tmpYear = tmpDate.getFullYear();
var tmpMonth = tmpDate.getMonth();
var monthLiteral;

for (var i = 0; i < 12; i++) {
tmpDate.setMonth(tmpMonth + i);
tmpDate.setFullYear(tmpYear);
monthLiteral = monthNames[tmpMonth];

months.push(monthLiteral + ' ' + tmpYear);

tmpYear = (tmpMonth == 11) ? tmpYear + 1 : tmpYear;
tmpMonth = (tmpMonth == 11) ? 0 : tmpMonth + 1;
}

return months;
};
}

最佳答案

不要试图操纵 Date 对象 - 只需使用它来获取月份和年份的初始值,然后对结果使用简单的算术:

function getNext12MonthNamesWithYear() {
var now = new Date();
var month = now.getMonth();
var year = now.getFullYear();

var names = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];

var res = [];
for (var i = 0; i < 13; ++i) {
res.push(names[month] + ' ' + year);
if (++month === 12) {
month = 0;
++year;
}
}
return res;
}

工作演示在 http://jsfiddle.net/alnitak/SQQdg/

关于javascript - 在 Javascript 中获取下一个 12 个月的结果搞砸了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10832179/

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