2011-04-21 112 views
26

此腳本有什麼問題?javascript日期+ 7天

當我設置我的時鐘說29/04/2011它增加了36/4/2011在一週輸入!但正確的日期應該是2011年6月5日

var d = new Date(); 
var curr_date = d.getDate(); 
var tomo_date = d.getDate()+1; 
var seven_date = d.getDate()+7; 
var curr_month = d.getMonth(); 
curr_month++; 
var curr_year = d.getFullYear(); 
var tomorrowsDate =(tomo_date + "/" + curr_month + "/" + curr_year); 
var weekDate =(seven_date + "/" + curr_month + "/" + curr_year); 
{ 
jQuery("input[id*='tomorrow']").val(tomorrowsDate); 
jQuery("input[id*='week']").val(weekDate); 
    } 
+4

'd.getDate()'給你一個整數...它不再是某種日期對象在這一點上... – michelgotta 2011-04-21 08:48:01

+1

我不得不建議只讀通過你在那裏做什麼,並在紙上工作,你的錯誤將是顯而易見的給你。 – Lazarus 2011-04-21 08:48:39

回答

0

兩個問題在這裏:

  1. seven_date是一個數字,而不是一個日期。 29 + 7 = 36
  2. getMonth返回該月份的基於零的索引。因此,添加一個會讓你獲得當前的月份數字。
+0

嗯,好吧,那麼如何使用最佳實踐來完成這一任何想法? – user472285 2011-04-21 08:50:57

+0

通常你會想在日期對象上使用一些setter方法(對於這個月,你會在12月遇到類似的問題)。然後,您可以將生成的Date對象轉換爲字符串。 – 2011-04-21 08:54:21

9

是這樣的嗎?

var days = 7; 
var date = new Date(); 
var res = date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 
alert(res); 

轉換爲再次約會:

date = new Date(res); 
alert(date) 

或者:

date = new Date(res); 

// hours part from the timestamp 
var hours = date.getHours(); 

// minutes part from the timestamp 
var minutes = date.getMinutes(); 

// seconds part from the timestamp 
var seconds = date.getSeconds(); 

// will display time in 10:30:23 format 
var formattedTime = date + '-' + hours + ':' + minutes + ':' + seconds; 
alert(formattedTime) 
+1

它returs 1303980939436 – user472285 2011-04-21 08:55:52

+0

嘗試警報(日期) – adam77 2011-04-21 09:02:31

0

使用Date對象的方法可以派上用場。

如:

myDate = new Date(); 
plusSeven = new Date(myDate.setDate(myDate.getDate() + 7)); 
+0

對我不起作用... – michelgotta 2011-04-21 08:53:36

+0

它返回1303980894137/4/2011 – user472285 2011-04-21 08:55:14

+3

*自我厭惡的洗牌* – trickwallett 2011-04-21 09:07:08

70
var date = new Date(); 
date.setDate(date.getDate() + 7); 

var dateMsg = date.getDate()+'/'+ (date.getMonth()+1) +'/'+date.getFullYear(); 
alert(dateMsg); 
+3

如果您添加超過31天,甚至可以工作! – Geo 2013-09-03 18:24:42

+0

什麼現代社會使用超過31天? – CodeMonkey 2016-10-10 17:35:04

+0

@CodeMonkey 32天?:D – 2017-01-22 05:58:40

5

最簡單的方法獲得一個約會x天以後是遞增日期:

function addDays(dateObj, numDays) { 
    return dateObj.setDate(dateObj.getDate() + numDays); 
} 

注意,這個修改提供的日期對象,例如

function addDays(dateObj, numDays) { 
    dateObj.setDate(dateObj.getDate() + numDays); 
    return dateObj; 
} 

var now = new Date(); 
var tomorrow = addDays(new Date(), 1); 
var nextWeek = addDays(new Date(), 7); 

alert(
    'Today: ' + now + 
    '\nTomorrow: ' + tomorrow + 
    '\nNext week: ' + nextWeek 
); 
+0

這是很好的,它似乎做我想要的,但它返回這樣的日期週五2011年05月06日11:07:01 GMT + 0200但我想日期是以這種格式dd/mm/year像這樣: 06/05/2011 – user472285 2011-04-21 09:08:57

2
var days = 7; 
var date = new Date(); 
var res = date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 

var d = new Date(res); 
var month = d.getMonth() + 1; 
var day = d.getDate(); 

var output = d.getFullYear() + '/' + 
    (month < 10 ? '0' : '') + month + '/' + 
    (day < 10 ? '0' : '') + day; 

$('#txtEndDate').val(output); 
1

您可以添加或增加一週的某一天爲下面的示例中,希望這會爲you.Lets看到有幫助....

 //Current date 
     var currentDate = new Date(); 
     //to set Bangladeshi date need to add hour 6   

     currentDate.setUTCHours(6);    
     //here 2 is day increament for the date and you can use -2 for decreament day 
     currentDate.setDate(currentDate.getDate() +parseInt(2)); 

     //formatting date by mm/dd/yyyy 
     var dateInmmddyyyy = currentDate.getMonth() + 1 + '/' + currentDate.getDate() + '/' + currentDate.getFullYear();