在JavaScript中增加日期

我需要在JavaScript中将日期值增加一天。

例如,我的日期值为2010-09-11,我需要将第二天的日期存储在JavaScript变量中。

如何将日期增加一天?

Pro小卤蛋A2020/03/17 11:44:13

Timezone/daylight savings aware date increment for JavaScript dates:

function nextDay(date) {
    const sign = v => (v < 0 ? -1 : +1);
    const result = new Date(date.getTime());
    result.setDate(result.getDate() + 1);
    const offset = result.getTimezoneOffset();
    return new Date(result.getTime() + sign(offset) * offset * 60 * 1000);
}
猪猪十三2020/03/17 11:44:13

Via native JS, to add one day you may do following:

let date = new Date(); // today
date.setDate(date.getDate() + 1) // tomorrow

Another option is to use moment library:

const date = moment().add(14, "days").toDate()
宝儿阿飞2020/03/17 11:44:13

Not entirelly sure if it is a BUG(Tested Firefox 32.0.3 and Chrome 38.0.2125.101), but the following code will fail on Brazil (-3 GMT):

Date.prototype.shiftDays = function(days){    
  days = parseInt(days, 10);
  this.setDate(this.getDate() + days);
  return this;
}

$date = new Date(2014, 9, 16,0,1,1);
$date.shiftDays(1);
console.log($date+"");
$date.shiftDays(1);
console.log($date+"");
$date.shiftDays(1);
console.log($date+"");
$date.shiftDays(1);
console.log($date+"");

Result:

Fri Oct 17 2014 00:01:01 GMT-0300
Sat Oct 18 2014 00:01:01 GMT-0300
Sat Oct 18 2014 23:01:01 GMT-0300
Sun Oct 19 2014 23:01:01 GMT-0200

Adding one Hour to the date, will make it work perfectly (but does not solve the problem).

$date = new Date(2014, 9, 16,0,1,1);

Result:

Fri Oct 17 2014 01:01:01 GMT-0300
Sat Oct 18 2014 01:01:01 GMT-0300
Sun Oct 19 2014 01:01:01 GMT-0200
Mon Oct 20 2014 01:01:01 GMT-0200
小宇宙LEY2020/03/17 11:44:13

Get the string value of the date using the dateObj.toJSON() method Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON Slice the date from the returned value and then increment by the number of days you want.

var currentdate = new Date();
currentdate.setDate(currentdate.getDate() + 1);
var tomorrow = currentdate.toJSON().slice(0,10);
小胖阳光2020/03/17 11:44:13

I feel that nothing is safer than .getTime() and .setTime(), so this should be the best, and performant as well.

const d = new Date()
console.log(d.setTime(d.getTime() + 1000 * 60 * 60 * 24)) // MILLISECONDS

.setDate() for invalid Date (like 31 + 1) is too dangerous, and it depends on the browser implementation.

西门蛋蛋2020/03/17 11:44:13
 Date.prototype.AddDays = function (days) {
    days = parseInt(days, 10);
    return new Date(this.valueOf() + 1000 * 60 * 60 * 24 * days);
}

Example

var dt = new Date();
console.log(dt.AddDays(-30));
console.log(dt.AddDays(-10));
console.log(dt.AddDays(-1));
console.log(dt.AddDays(0));
console.log(dt.AddDays(1));
console.log(dt.AddDays(10));
console.log(dt.AddDays(30));

Result

2017-09-03T15:01:37.213Z
2017-09-23T15:01:37.213Z
2017-10-02T15:01:37.213Z
2017-10-03T15:01:37.213Z
2017-10-04T15:01:37.213Z
2017-10-13T15:01:37.213Z
2017-11-02T15:01:37.213Z
达蒙十三2020/03/17 11:44:13

Two methods:

1:

var a = new Date()
// no_of_days is an integer value
var b = new Date(a.setTime(a.getTime() + no_of_days * 86400000)

2: Similar to the previous method

var a = new Date()
// no_of_days is an integer value
var b = new Date(a.setDate(a.getDate() + no_of_days)
村村LEY2020/03/17 11:44:12

Getting the next 5 days:

var date = new Date(),
d = date.getDate(),
m = date.getMonth(),
y = date.getFullYear();


for(i=0; i < 5; i++){
var curdate = new Date(y, m, d+i)
console.log(curdate)
}
Itachi理查德2020/03/17 11:44:12
var myDate = new Date();

//add a day to the date
myDate.setDate(myDate.getDate() + 1);
Eva宝儿理查德2020/03/17 11:44:12

最简单的方法是转换为毫秒并添加1000 * 60 * 60 * 24毫秒,例如:

var tomorrow = new Date(today.getTime()+1000*60*60*24);