我正在尝试从一个月中的某一天开始循环获取日期。该代码在前几天有效,从那时起它返回随机日期。
这里有什么帮助吗?
var now = new Date();
var initDate = new Date(now.getYear() + 1900, 0);
for (var i = 1 ; i <366 ; i++) {
var day = dateFromDay(i, initDate);
document.write(" Day " + i + " : " + day);
document.write("<br>");
}
function dateFromDay(day, initDate) {
var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
var cur = new Date(initDate.setDate(day));
cur = months[cur.getMonth()] + " " + cur.getDate();
return cur;
}
输出:
...
...
Day 29 : Jan 29
Day 30 : Jan 30
Day 31 : Jan 31
Day 32 : Feb 1
// From here the output gets messed up
Day 33 : Mar 4
Day 34 : Apr 3
Day 35 : May 5
Day 36 : Jun 5
Day 37 : Jul 7
Day 38 : Aug 7
Day 39 : Sep 8
Day 40 : Oct 10
Day 41 : Nov 10
Day 42 : Dec 12
Day 43 : Jan 12
Day 44 : Feb 13
...
...