Javascript 将日期添加到 Date 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6963311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add day(s) to a Date object
提问by iLemming
Possible Duplicate:
How to add number of days to today's date?
可能的重复:
如何将天数添加到今天的日期?
I'm confused, I found so many different approaches, and which of them is actually correct?
我很困惑,我发现了这么多不同的方法,其中哪一个实际上是正确的?
So what is the correct way to add day(s) to a given Date?
那么将日期添加到给定日期的正确方法是什么?
回答by nobody
date.setTime( date.getTime() + days * 86400000 );
回答by user710502
Note: Use it if calculating / adding days from current date.
注意:如果从当前日期计算/添加天数,请使用它。
Be aware: this answer has issues (see comments)
请注意:这个答案有问题(见评论)
var myDate = new Date();
myDate.setDate(myDate.getDate() + AddDaysHere);
It should be like
它应该像
var newDate = new Date(date.setTime( date.getTime() + days * 86400000 ));
var newDate = new Date(date.setTime( date.getTime() + days * 86400000 ));