Javascript 如何在javascript中为现在日期添加天数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11338862/
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
How to add days to the now date in javascript?
提问by 250091017
var myDate = new Date();
var endtime= new Date(myDate.getDate()+1,23:59:59);
alert(endtime);
why there is no value of the endtime? if i want to add 1 day 10 hours 50 minute 30 second to the now time, how to wirte the endtime code? thank you
为什么没有结束时间的价值?如果我想将 1 天 10 小时 50 分 30 秒添加到现在时间,如何编写结束时间代码?谢谢你
回答by Pranay Rana
回答by Nikson Kanti Paul
try this
尝试这个
var date = new Date();
var numberToAdd = 1;
date.setDate(date.getDate() + numberToAdd);
回答by Bergi
You will need to add the days in milliseconds:
您需要以毫秒为单位添加天数:
var tomorrow = new Date(Date.now() + 1 * 24*3600*1000);
Of course you can add various amounts of time, you just need to count it in milliseconds when using the Date constructor or set/getTime().
当然你可以添加不同的时间量,你只需要在使用 Date 构造函数或 set/getTime() 时以毫秒为单位计算它。
You can also set the different units one-by-one using their respective Date
methods:
您还可以使用各自的Date
方法一一设置不同的单位:
var sometime = new Date; // now
sometime.setDate(sometime.getDate() + numberOfDays);
sometime.setHours(sometime.getHours() + numberOfHours);
sometime.setMinutes(sometime.getMinutes() + numberOfMinutes);
...
You can't set the Date with a float value, it will be truncated when beeing converted to an Integer.
您不能使用浮点值设置日期,当转换为整数时它会被截断。
But the setter methods higher than milliseconds and higher than date have optional attributes, so that you can combine the setting:
但是高于毫秒和高于日期的 setter 方法具有可选属性,以便您可以组合设置:
var sometime = new Date; // now
sometime.setFullYear(
sometime.getFullYear() + numberOfYears,
sometime.getMonth() + numberOfMonths,
sometime.getDate() + numberOfDays
);
sometime.setHours(
sometime.getHours() + numberOfHours,
sometime.getMinutes() + numberOfMinutes,
...
);
回答by Jacobi
I always create 7 functions, to work with date in JS: addSeconds, addMinutes, addHours, addDays, addWeeks, addMonths, addYears.
我总是创建 7 个函数来处理 JS 中的日期:addSeconds、addMinutes、addHours、addDays、addWeeks、addMonths、addYears。
You can see an example here: http://jsfiddle.net/tiagoajacobi/YHA8x/
你可以在这里看到一个例子:http: //jsfiddle.net/tiagoajacobi/YHA8x/
How to use:
如何使用:
var now = new Date();
console.log(now.addWeeks(3));
This are the functions:
这是功能:
Date.prototype.addSeconds = function(seconds) {
this.setSeconds(this.getSeconds() + seconds);
return this;
};
Date.prototype.addMinutes = function(minutes) {
this.setMinutes(this.getMinutes() + minutes);
return this;
};
Date.prototype.addHours = function(hours) {
this.setHours(this.getHours() + hours);
return this;
};
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + days);
return this;
};
Date.prototype.addWeeks = function(weeks) {
this.addDays(weeks*7);
return this;
};
Date.prototype.addMonths = function (months) {
var dt = this.getDate();
this.setMonth(this.getMonth() + months);
var currDt = this.getDate();
if (dt !== currDt) {
this.addDays(-currDt);
}
return this;
};
Date.prototype.addYears = function(years) {
var dt = this.getDate();
this.setFullYear(this.getFullYear() + years);
var currDt = this.getDate();
if (dt !== currDt) {
this.addDays(-currDt);
}
return this;
};