从今天开始,我有以下代码用于生成未来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;
};
}
Run Code Online (Sandbox Code Playgroud)
调用的结果getNext12MonthNamesWithYear()是:
尽可能地,列表的开头有点奇怪,因为"六月"缺失,加上"五月","七月"和"八月"出现两次.
我自然会在这里做错事; 有人可以帮帮我吗?
编辑:
基于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;
};
}
Run Code Online (Sandbox Code Playgroud)
不要试图操纵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;
}
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/alnitak/SQQdg/的工作演示
这是我使用Moment.js 的解决方案
未来 12 个月
let months = [];
let monthsRequired = 12
for (let i = 1; i <= monthsRequired; i++) {
months.push( moment().add(i, 'months').format('MMMM YYYY') )
}
console.log(months)Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>Run Code Online (Sandbox Code Playgroud)
以防万一,如果您需要前 12 个月
let months = [];
let monthsRequired = 12
for (let i = monthsRequired; i >= 1; i--) {
months.push( moment().subtract(i, 'months').format('MMMM YYYY') )
}
console.log(months)Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>Run Code Online (Sandbox Code Playgroud)
我不明白为什么这行不通
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);
tmpMonth = (tmpMonth == 11) ? 0 : tmpMonth+1;
tmpYear = (tmpMonth == 11) ? tmpYear+1 : tmpYear;
}
return months;
};
}
Run Code Online (Sandbox Code Playgroud)