2017-04-13 76 views
0
<code> 
    var d2 = $('#interval').val(); 
    var new_date = new Date(get_start_date); 
    new_date.setDate(new_date.getDate() + d2); 
    var dd = new_date.getDate(); 
    var mm = new_date.getMonth() + 1; 
    var y = new_date.getFullYear(); 
    var endDate = y + '-' + mm + '-' + dd; 
    </code> 

假設D2 = 5的Javascript的setDate犯規接受輸入

時,即時通訊增加5個日期以我目前的日期,它沒有返回確切的答案,而不是它的加入月變得2017年9月9日 但是當我只是這樣做new_date.setDate(new_date.getDate()+ 5)它給了我正確的輸出。

+0

嘗試'VAR D2 =編號($( '#間隔')。VAL())' – gurvinder372

+0

你確保你能獲得d2的正確值?它給你的字符串不是數字,只是解析它作爲一個數字 –

+0

@ gurvinder372做了詭計!十分感謝! – PEENGEE

回答

1

只是解析您D2爲整數

var d2 = parseInt($('#interval').val(),10);

0

ü只能使用此參數 新的Date() 新的日期(毫秒) 新的日期(dateString) 新的日期(年,月,日,時,分,秒,毫秒)

get_start_date是invailid)

變種NEW_DATE =新日期(get_start_date);