Javascript 在日期边界案例中添加一天?
我不明白为什么这个代码不起作用。因此,解决方案提到执行Javascript 在日期边界案例中添加一天?,javascript,date,Javascript,Date,我不明白为什么这个代码不起作用。因此,解决方案提到执行date.getDate()+1应该增加一天,但在我的情况下,它会增加一个月零两天 var year = 2014; var month = 3; var day = 31; // creating an actual date requestedDate = new Date(year, month - 1, day); console.debug(requestedDate.toString()); // outputs "Mon Ma
date.getDate()+1
应该增加一天,但在我的情况下,它会增加一个月零两天
var year = 2014;
var month = 3;
var day = 31;
// creating an actual date
requestedDate = new Date(year, month - 1, day);
console.debug(requestedDate.toString());
// outputs "Mon Mar 31 2014 00:00:00 GMT+0200 (CEST)"
var d = new Date();
d.setDate(requestedDate.getDate()+1);
console.debug(d.toString());
// outputs "Fri May 02 2014 11:04:52 GMT+0200 (CEST)"
您没有将第二个日期设置为与第一个日期相同 在第一个
newdate()
中,您将日期设置为31。三月。第二个
new Date()
将日期设置为今天,1。四月。31+1=32
,和1。4月加上32天应该是2天。五月
var year = 2014;
var month = 3;
var day = 31;
// creating an actual date
requestedDate = new Date(year, month - 1, day);
console.debug(requestedDate.toString());
var d = new Date(year, month - 1, day); // set the date to the same
d.setDate(requestedDate.getDate()+1);
console.debug(d.toString());
因为月份是以0为基础的。一月=0,二月=1,三月=2,四月=3。。。而且,
d
从未设置为特定日期<代码>新日期()等于当前日期。与请求日期不同。是的,我知道,这就是我在第6行减去1的原因。谢谢!我认为setDate()
就足够了。我会尽快接受你的答复。