下一个12个月使用Javascript的结果搞砸了 [英] Result of getting next 12 months in Javascript is messed up

查看:57
本文介绍了下一个12个月使用Javascript的结果搞砸了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code for generating a list of the next 12 months (inclusive), starting from today's date:

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月

  • 11月2012

  • 2012年12月

  • 2013年1月

  • 2013年2月

  • 2013年3月

  • 2013年4月

  • 2013年5月

  • "May 2012"
  • "July 2012"
  • "August 2012"
  • "May 2012"
  • "July 2012"
  • "August 2012"
  • "September 2012"
  • "October 2012"
  • "November 2012"
  • "December 2012"
  • "January 2013"
  • "February 2013"
  • "March 2013"
  • "April 2013"
  • "May 2013"

如您所愿,列表的开头有点奇怪,因为缺少六月,加上出现了五月,七月和八月

As you can, the start of the list is a bit weird, in that "June" is missing, plus "May", "July" and "August" appear twice.

自然,我在这里做错了什么;

Naturally I;m doing something very wrong here; could someone please help me out?

编辑:

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

Based on micadelli's comment, here is the solution I used:

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


推荐答案

我看不到任何原因

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 Bin

http://jsbin.com/aqezom

这篇关于下一个12个月使用Javascript的结果搞砸了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆