如何使用JavaScript将日期添加到当前日期?JavaScript是否有像.NET的AddDay()那样的内置函数?
当前回答
正确答案:
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>
其他回答
我已经用这种方法在一行中找到了正确的日期,以获得时间加上人们上面所说的一天。
((new Date()).setDate((new Date()).getDate()+1))
我只是想建立一个正常的(新日期()):
(new Date()).getDate()
> 21
使用上面的代码,我现在可以在(newDate())中的Date()中设置所有这些,并且它的行为正常。
(new Date(((new Date()).setDate((new Date()).getDate()+1)))).getDate()
> 22
或获取Date对象:
(new Date(((new Date()).setDate((new Date()).getDate()+1))))
您可以使用JavaScript,不需要jQuery:
var someDate = new Date();
var numberOfDaysToAdd = 6;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
Formatting to dd/mm/yyyy :
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();
var someFormattedDate = dd + '/'+ mm + '/'+ y;
new Date(Date.now() + 2000 * 86400)
此代码段使用“2000”参数为当前日期添加两天。您可以通过更新第二个参数中的“2000”值来调整天数。
您可以使用这种单行格式,使用本机JavaScript日期将日期添加到当前日期。
我的测试示例可以在日期对象的同一实例中执行减号。
Date.prototype.reset=函数(){let newDate=新日期(this.timeStamp)this.setFullYear(newDate.getFullYear)this.setMonth(newDate.getMonth())this.setDate(newDate.getDate())this.setHours(newDate.getHours())this.set分钟(newDate.getMinutes())this.setSeconds(newDate.getSeconds())this.set毫秒(newDate.getMilliseconds())}Date.prototype.addDays=函数(天){this.timeStamp=此[Symbol.toPrimitive]('编号')let daysInMiliseconds=(天*(1000*60*60*24))this.timeStamp=this.timeStamp+天毫秒this.reset()}Date.prototype.minusDays=函数(天){this.timeStamp=此[Symbol.toPrimitive]('编号')let daysInMiliseconds=(天*(1000*60*60*24))如果(daysInMiliseconds<=this.timeStamp){this.timeStamp=this.timeStamp-天毫秒this.reset()}}var temp=新日期(Date.now())//从现在开始console.log(temp.toDateString())临时添加天数(31)console.log(temp.toDateString())温度-天(5)console.log(temp.toDateString())
您可以在此处创建自定义助手函数
function plusToDate(currentDate, unit, howMuch) {
var config = {
second: 1000, // 1000 miliseconds
minute: 60000,
hour: 3600000,
day: 86400000,
week: 604800000,
month: 2592000000, // Assuming 30 days in a month
year: 31536000000 // Assuming 365 days in year
};
var now = new Date(currentDate);
return new Date(now + config[unit] * howMuch);
}
var today = new Date();
var theDayAfterTommorow = plusToDate(today, 'day', 2);
顺便说一句,这是一个通用的解决方案,可以根据需要添加秒、分钟或天。
推荐文章
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法