5

我有以下代码用于生成从今天开始的未来 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 月”

如您所知,列表的开头有点奇怪,因为缺少“六月”,加上“五月”、“七月”和“八月”出现了两次。

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

编辑:

根据 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;
    };
}
4

5 回答 5

11

不要试图操纵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/工作演示

于 2012-05-31T12:29:12.000 回答
7

这是我使用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)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

以防万一您需要前 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)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

于 2017-08-17T18:29:51.040 回答
4

我看不出这不起作用的任何原因

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;
   };
}

JS斌
http://jsbin.com/aqezom

于 2012-05-31T11:26:56.057 回答
3

这是一个工作版本。如果值大于 12,则不能直接执行 setMonth。此外,某些月份没有 31 日,这会导致意外结果。JS 在这里提琴 FIDDLE

    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";

var today = new Date();
var currentMonth = today.getMonth();
var i;
for (i = 0 ; i < 12 ; i++) {
    var newMonth = currentMonth + i;
    var newYear = newMonth > 11 ? today.getFullYear() + 1: today.getFullYear();
    newMonth = newMonth > 11? (newMonth - 12): newMonth;

    var newDate = new Date(newYear, newMonth, '1');

    console.log(monthNames[newDate.getMonth()] + ' ' + newDate.getFullYear());
}

​
于 2012-05-31T12:08:14.110 回答
1

我还需要一份过去 12 个月的清单,这就是我所做的:

var theMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var today = new Date();
var aMonth = today.getMonth();
var i;
for (i=0; i<12; i++) {
    document.writeln(theMonths[aMonth] + '<br>'); //here you can do whatever you want...
    aMonth++;
    if (aMonth > 11) {
        aMonth = 0;
    }
}
于 2015-02-25T09:47:31.880 回答