2011-04-21 114 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();