2016-02-19 88 views
0

我添加到11日至2016年2月19日开始日期和添加11日后它成为2016年3月1日,但是,当我运行getMonth()JavaScript函数返回feb而不是行军JavaScript添加日期,但它显示旧的日期月

这里是我的代码

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变量是星期二2016年3月1日00:00:00 GMT + 0500比为什么它返回月份为2 任何建议将是有益的感谢提前

+1

蒙特hs是基于JavaScript的零。一月是0,二月是1,... – j08691

回答

1

getMonth()方法根据当地时间将指定日期 中的月份作为基于零的值(其中零表示 年的第一个月)返回。 getMonth()返回的值是一个介于0和11之间的整数。0对应于一月份,一月份到二月份,依此类推。

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

Source

+0

所以加1 + secondSeasonDaysHere.getMonth()+ 1 – FahadAkram

+0

是好的,如果你想获得它的十进制值。如果你想要月份的名字,你不会添加任何东西,并会做'var monthName = months [secondSeasonDaysHere.getMonth()]' – bitten

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]);