获取给定日期后的第n天的日期

我正在寻找某个特定日期之后第100天的日期。有没有办法通过javascript做到这一点?

我希望它可以像下面一样简单,但是并不是那么简单。

  var givenDay = new Date(01/01/2020);
  var hundredthDay = new Date(birthday + 100);
  console.log(hundrethDay)
评论
stotam
stotam

You can try using .setDate() and .getDate() combination.

The setDate() method sets the day of the Date object relative to the beginning of the currently set month.

The getDate() method returns the day of the month for the specified date according to local time.

Adding the required days to .getDate() as the following:

const givenDay = new Date('01-01-2020');
console.log(givenDay);

const result = new Date(givenDay.setDate(givenDay.getDate() + 100));
console.log(result);

我希望这有帮助!

点赞
评论