2016-02-19 36 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]);