如何使用JavaScript将日期添加到当前日期?JavaScript是否有像.NET的AddDay()那样的内置函数?
当前回答
感谢Jason的回答,您的回答符合预期,这里是您的代码和AnthonyWJones的便捷格式的混合:
Date.prototype.addDays = function(days){
var ms = new Date().getTime() + (86400000 * days);
var added = new Date(ms);
return added;
}
其他回答
我们的团队认为date fns是这个领域最好的图书馆。它将日期视为不可变的(Moment.js可能永远不会采用不变性),速度更快,可以模块化加载。
const newDate = DateFns.addDays(oldDate, 2);
我的简单解决方案是:
nextday=new Date(oldDate.getFullYear(),oldDate.getMonth(),oldDate.getDate()+1);
这种解决方案在夏时制方面没有问题。此外,还可以添加/减去年、月、日等的任何抵销。
day=new Date(oldDate.getFullYear()-2,oldDate.getMonth()+22,oldDate.getDate()+61);
是正确的代码。
我昨晚创建了这些扩展:可以传递正值或负值;
例子:
var someDate = new Date();
var expirationDate = someDate.addDays(10);
var previous = someDate.addDays(-5);
Date.prototype.addDays = function (num) {
var value = this.valueOf();
value += 86400000 * num;
return new Date(value);
}
Date.prototype.addSeconds = function (num) {
var value = this.valueOf();
value += 1000 * num;
return new Date(value);
}
Date.prototype.addMinutes = function (num) {
var value = this.valueOf();
value += 60000 * num;
return new Date(value);
}
Date.prototype.addHours = function (num) {
var value = this.valueOf();
value += 3600000 * num;
return new Date(value);
}
Date.prototype.addMonths = function (num) {
var value = new Date(this.valueOf());
var mo = this.getMonth();
var yr = this.getYear();
mo = (mo + num) % 12;
if (0 > mo) {
yr += (this.getMonth() + num - mo - 12) / 12;
mo += 12;
}
else
yr += ((this.getMonth() + num - mo) / 12);
value.setMonth(mo);
value.setYear(yr);
return value;
}
最简单的解决方案。
Date.prototype.addDays=函数(天){this.setDate(this.getDate()+parseInt(天));返回此;};//然后打电话var newDate=新日期().addDays(2)//+2天console.log(newDate);//或var newDate1=新日期().addDays(-2)//-2天console.log(newDate1);
正确答案:
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
回答不正确:
这个答案有时提供正确的结果,但往往返回错误的年份和月份。这个答案唯一有效的时间是您要添加天数的日期恰好是当前的年份和月份。
// Don't do it this way!
function addDaysWRONG(date, days) {
var result = new Date();
result.setDate(date.getDate() + days);
return result;
}
证明/示例
检查此JsFidle
//正确函数addDays(日期,天){var result=新日期(Date);result.setDate(result.getDate()+天);返回结果;}//坏年份/月函数addDaysWRONG(日期,天){var result=新日期();result.setDate(date.getDate()+天);返回结果;}//DST期间不良函数addDaysDstFail(日期,天){var dayms=(天*24*60*60*1000);返回新日期(Date.getTime()+dayms);}//测试函数formatDate(日期){return(date.getMonth()+1)+'/'+date.getDate()+'/'+date.getFullYear();}$('tbody-td:firstchild').each(函数(){var$in=$(此);var$out=$('<td/>').insertAfter($in).addClass(“answer”);var$outFail=$('<td/>').insertAfter($out);var$outDstFail=$('<td/>').insertAfter($outFail);var date=新日期($in.text());var correctDate=formatDate(addDays(日期,1));var failDate=formatDate(addDaysWRONG(日期,1));var failDstDate=formatDate(addDaysDstFail(日期,1));$out.text(correctDate);$outFail.text(失败日期);$outDstFail.text(failDstDate);$outFail.addClass(correctDate==failDate?“right”:“error”);$outDstFail.addClass(correctDate==failDstDate?“right”:“error”);});正文{字体大小:14px;}表{边界塌陷:塌陷;}表格,td,th{边框:1px实心黑色;}标准差{填充:2px;}.错误{颜色:红色;}.对{颜色:绿色;}.答案{字号:粗体;}<script src=“https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js“></script><表><tbody><tr>夏令时日期</th></tr><tr><th>输入</th><th>+1天</th><th>+1天失败</th><th>+1天DST失败</th></tr><tr><td>2013年10月3日</td></tr><tr><td>2013年3月11日</td></tr><tr><td>2014年9月3日</td></tr><tr><td>2014年2月11日</td></tr><tr><td>2015年8月3日</td></tr><tr><td>2015年1月11日</td></tr><tr><th colspan=“4”>2013</th></tr><tr><th>输入</th><th>+1天</th><th>+1天失败</th><th>+1天DST失败</th></tr><tr><td>2013年1月1日</td></tr><tr><td>2013年1月2日</td></tr><tr><td>2013年1月3日</td></tr><tr><td>2013年1月4日</td></tr><tr><td>2013年1月5日</td></tr><tr><td>2013年1月6日</td></tr><tr><td>2013年1月7日</td></tr><tr><td>2013年1月8日</td></tr><tr><td>2013年1月9日</td></tr><tr><td>1013年1月10日</td></tr><tr><td>2013年1月11日</td></tr><tr><td>2013年1月12日</td></tr><tr><th colspan=“4”>2014</th></tr><tr><th>输入</th><th>+1天</th><th>+1天失败</th><th>+1天DST失败</th></tr><tr><td>2014年1月1日</td></tr><tr><td>2014年1月2日</td></tr><tr><td>2014年1月3日</td></tr><tr><td>2014年1月4日</td></tr><tr><td>2014年1月5日</td></tr><tr><td>2014年1月6日</td></tr><tr><td>2014年1月7日</td></tr><tr><td>2014年1月8日</td></tr><tr><td>2014年1月9日</td></tr><tr><td>1014年1月10日</td></tr><tr><td>2014年1月11日</td></tr><tr><td>2014年1月12日</td></tr><tr><th colspan=“4”>2015</th></tr><tr><th>输入</th><th>+1天</th><th>+1天失败</th><th>+1天DST失败</th></tr><tr><td>2015年1月1日</td></tr><tr><td>2015年1月2日</td></tr><tr><td>2015年1月3日</td></tr><tr><td>2015年1月4日</td></tr><tr><td>2015年1月5日</td></tr><tr><td>2015年1月6日</td></tr><tr><td>2015年1月7日</td></tr><tr><td>2015年1月8日</td></tr><tr><td>2015年1月9日</td></tr><tr><td>2015年1月10日</td></tr><tr><td>2015年1月11日</td></tr><tr><td>2015年1月12日</td></tr></tbody></table>