JavaScript日期+7天

88

这个脚本有什么问题?

当我将我的时钟设置为2011年4月29日时,它会在周输入框中添加2011年4月36日,但实际上正确的日期应该是2011年5月6日

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
3
我建议你仔细阅读自己正在做的事情,并在纸上进行推敲,这样你的错误就会显而易见。 - Lazarus
10个回答

197

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

console.log(date);

是的,即使date.getDate() + 7大于该月的最后一天,这也是有效的。查看MDN以获取更多信息。


1
现代社会使用超过31天的是什么? - CodeMonkey
11
超过一个月的那些。 - treyBake
请问,我如何仅以YYYY-mm-dd格式获取此结果?我需要将结果注入到input type="date"中 :) - Mego

42

未声明

返回时间戳

new Date().setDate(new Date().getDate() + 7)

返回日期

new Date(new Date().setDate(new Date().getDate() + 7))

16

像这样吗?

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
这是Unix时间戳 - 您可以将其转换为可读的日期和时间格式 - 我们认为这是最佳实践。 - ezmilhouse
第二段中的赋值是多余的,当您调用setTime时,“date”已更新。 - adam77

13
简述:
new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)

这并没有提供问题的答案。如果要批评或请求作者澄清,请在他们的帖子下留言。-【来自审查】 - nurdyguy
3
他想知道为什么他的代码无法运行。暗示地,他也想知道如何获取七天后的日期。我提供了答案。 - MarcoLe
在我看来,这段代码完全没有解释,更适合作为评论而不是答案,所以我标记了它。同时也可以注意到,这本质上是 Omid Matouri 在将近5年前给出的答案的压缩重述。也许我错了,这是一个有效的答案。但至少应该将其评为非常糟糕的答案。 - nurdyguy
5
让那个人轻松点,这是一个完美简洁的一行代码,可以完成任务而不需要使用混乱的临时变量。 - Adam
2
谢谢!这正是我所需要的。我稍微修改了一下以适应我的需求,我需要一个纪元时间,所以我使用了:Math.floor((Date.now() + 7 * 24 * 60 * 60 * 1000)/1000) -- 希望对其他人也有用! - Sensoray
显示剩余2条评论

7
简单的方法是通过增加日期来获取未来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
);

这很不错,它似乎做了我想要的事情,但是它返回的日期格式是这样的:Fri May 06 2011 11:07:01 GMT+0200,但我希望日期以 dd/mm/year 的格式显示,就像这样:06/05/2011。 - user472285

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

2

使用Date对象的方法会很有用。

例如:

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

它返回1303980894137/4/2011 - user472285
6
shuffles off in self disgust” 的意思是“带着自我厌恶的神情慢慢地离开”。 - trickwallett

2

var future = new Date(); // get today date
future.setDate(future.getDate() + 7); // add 7 days
var finalDate = future.getFullYear() +'-'+ ((future.getMonth() + 1) < 10 ? '0' : '') + (future.getMonth() + 1) +'-'+ future.getDate();
console.log(finalDate);


1
你可以为以下示例添加或增加星期几,希望这对你有所帮助。让我们看看...
        // Current date
        var currentDate = new Date();
        // to set Bangladeshi date need to add hour 6           

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

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

0

这里有两个问题:

  1. seven_date 是一个数字,而不是日期。 29 + 7 = 36
  2. getMonth 返回的是月份的从零开始的索引。因此,加一只会得到当前月份的数字。

嗯,那么有没有使用最佳实践来完成这个任务的想法? - user472285
通常情况下,您会想要在日期对象上使用一些setter方法(对于月份也是如此,在12月份时您会遇到类似的问题)。然后,您可以将生成的Date对象转换为字符串。 - Jakub Hampl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接