如何使用JavaScript将日期添加到当前日期?JavaScript是否有像.NET的AddDay()那样的内置函数?
当前回答
我想我也会给出答案:就我个人而言,我喜欢尝试避免无谓的变量声明、方法调用和构造函数调用,因为它们的性能都很昂贵。(当然是合理的)我本打算在@AnthonyWJones给出的答案下留下评论,但考虑得更好。
// Prototype usage...
Date.prototype.addDays = Date.prototype.addDays || function( days ) {
return this.setTime( 864E5 * days + this.valueOf() ) && this;
};
// Namespace usage...
namespace.addDaysToDate = function( date, days ) {
return date.setTime( 864E5 * days + date.valueOf() ) && date;
};
// Basic Function declaration...
function addDaysToDate( date, days ) {
return date.setTime( 864E5 * days + date.valueOf() ) && date;
};
以上内容将尊重DST。这意味着,如果您添加一个跨越夏令时的天数,则显示的时间(小时)将更改以反映这一点。例子:2014年11月2日02:00是夏令时的结束。
var dt = new Date( 2014, 10, 1, 10, 30, 0 );
console.log( dt ); // Sat Nov 01 2014 10:30:00
console.log( dt.addDays( 10 ) ); // Tue Nov 11 2014 09:30:00
如果你想保留夏令时的时间(所以10:30仍然是10:30)。。。
// Prototype usage...
Date.prototype.addDays = Date.prototype.addDays || function( days ) {
return this.setDate( this.getDate() + days ) && this;
};
// Namespace usage...
namespace.addDaysToDate = function( date, days ) {
return date.setDate( date.getDate() + days ) && date;
};
// Basic Function declaration...
function addDaysToDate( date, days ) {
return date.setDate( date.getDate() + days ) && date;
};
所以,现在你。。。
var dt = new Date( 2014, 10, 1, 10, 30, 0 );
console.log( dt ); // Sat Nov 01 2014 10:30:00
console.log( dt.addDays( 10 ) ); // Tue Nov 11 2014 10:30:00
其他回答
在java脚本中添加日期的非常简单的代码。
var d=新日期();d.setDate(d.getDate()+提示('你想在这里添加多少天'));警报(d);
new Date(Date.now() + 2000 * 86400)
此代码段使用“2000”参数为当前日期添加两天。您可以通过更新第二个参数中的“2000”值来调整天数。
您可以使用这种单行格式,使用本机JavaScript日期将日期添加到当前日期。
我已经用这种方法在一行中找到了正确的日期,以获得时间加上人们上面所说的一天。
((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))))
我的测试示例可以在日期对象的同一实例中执行减号。
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())
最简单的答案是,假设需要在当前日期上增加1天:
var currentDate = new Date();
var numberOfDayToAdd = 1;
currentDate.setDate(currentDate.getDate() + numberOfDayToAdd );
要逐行向您解释此代码的作用:
创建名为currentDate的当前日期变量。默认情况下,“new Date()”会自动将当前日期分配给变量。创建一个变量以保存要添加到日期的天数(您可以跳过此变量,直接使用第三行中的值)通过给定相同的值+所需的数字来更改Date的值(因为Date是保存在对象中的月份的日期)。切换到下个月将是自动的