javascript 日期 + 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4868241/
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
javascript date + 1
提问by Rod
How do I take today's date and add 1 day to it?
如何获取今天的日期并为其添加 1 天?
If possible, inline please?
如果可能,请内联?
回答by cambraca
This will get tomorrow's date:
这将得到明天的日期:
var a = new Date((new Date()).valueOf() + 1000*3600*24);
回答by Tom Tu
You have to use the getDate() and setDate() methods of the Date object which respectively get and set the day value of the date.
您必须使用 Date 对象的 getDate() 和 setDate() 方法,它们分别获取和设置日期的日期值。
var date = new Date();
date.setDate(date.getDate() + 1);
Check the MDC Date object referencefor more information on working with dates
查看 MDC 日期对象参考以获取有关使用日期的更多信息
回答by Pablo Santa Cruz
Try this:
尝试这个:
//create the date
var myDate = new Date();
//add a day to the date
myDate.setDate(myDate.getDate() + 1);
回答by John Giotta
dt = new Date();
dt.setDate(dt.getDate() + 1);
回答by zachelrath
If by "add 1 day to it" you mean "add 24 hours", that is, add 24*60*60*1000 milliseconds to a JavaScript date object, then the correct solution is:
如果“向其添加 1 天”的意思是“添加 24 小时”,即向 JavaScript 日期对象添加 24*60*60*1000 毫秒,那么正确的解决方案是:
var d = new Date();
d.setTime(d.getTime() + 86400000);
console.log('24 hours later');
console.log(d);
As @venkatagiri pointed out in an earlier comment, this will in fact add 24 hours to the current JavaScript date object in all scenarios, while d.setDate(d.getDate() + 1)
will NOT if a Daylight Savings Time cross-over is involved. See this JSFiddleto see the difference in context of the 2013 start of DST (at March 10, 2013 at 2:00 AM, DST locale time moved forward an hour). setDate()
in this scenario only adds 23 hours, while setTime()
adds 24.
正如@venkatagiri 在之前的评论中指出的那样,这实际上会在所有场景中为当前的 JavaScript 日期对象增加 24 小时,而d.setDate(d.getDate() + 1)
如果涉及夏令时交叉,则不会。请参阅此 JSFiddle以了解 2013 年 DST 开始的上下文差异(2013 年 3 月 10 日凌晨 2:00,DST 语言环境时间向前移动了一个小时)。setDate()
在这种情况下只增加 23 小时,而setTime()
增加 24。
回答by Abhisek Bose
var d = new Date();
var curr_date = d.getDate();
var n =curr_date;
jQuery(".class_name:eq(0)").text(n);
var m =[d.getDate()+1];
jQuery(".class_name:eq(1)").text(m);
回答by Mani
Add 30 days and set the date value to datepicker
添加 30 天并将日期值设置为 datepicker
Example :
例子 :
$(document).ready(function() {
var myDate = new Date();
//add a day to the date
myDate.setDate(myDate.getDate() + 30);
var end_date = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate());
$('#datepicker').datepicker({
format: 'dd-mm-yyyy',
orientation: 'bottom'
});
$('#datepicker').datepicker('setDate', end_date);
});