javascript添加日期,但它显示旧日期月

时间:2016-02-19 15:51:50

标签: javascript datetime

我将从2016年2月19日开始添加11天,在添加11天后,它将成为2016年3月1日,但是当我运行getMonth()javascript函数时,它返回feb而不是march

这是我的代码

var stDate = new Date($("#datepicker1").val()); 
//Fri Feb 19 2016 00:00:00 GMT+0500 (Pakistan Standard Time)
var secondSeasonDays = stDate.setDate(stDate.getDate() + firstSeasonDays);
//firstSeasonDays = 11
var secondSeasonDays = new Date(secondSeasonDays);
// it return  Tue Mar 01 2016 00:00:00 GMT+0500 (Pakistan Standard Time)
var secondSeasonDays = secondSeasonDays.getDate();
var secondSeasonMonth = secondSeasonDays.getMonth();
alert("second season start date is " + secondSeasonDays + " and start month is " + secondSeasonMonth );
// it alerts second season start date is 1 and start month is 2

而secondSeasonDaysHere Variable中的日期是2016年3月1日星期二00:00:00 GMT + 0500,而不是为什么它返回月份为2 任何建议都会提前感谢

2 个答案:

答案 0 :(得分:1)

  

getMonth()方法返回指定日期的月份   根据当地时间,作为从零开始的值(零表示   今年的第一个月)。 getMonth()返回的值是0到11之间的整数.0对应于1月,1到2月,依此类推。

var months = [ "January", "February", "March", "April" .. ];

Source

答案 1 :(得分:1)

在javascript月份从0到11开始。

使用数组作为

var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

然后

alert("second season start date is " + secondSeasonDays + " and start month is " + months[secondSeasonMonth] );