0

I am looking to find the date for, say, the 100th day after any given day. Is there a way to do that via javascript?

I was hoping it could be as simple as below, but it isn't quite that simple.

  var givenDay = new Date(01/01/2020);
  var hundredthDay = new Date(givenDay + 100);
  console.log(hundredthDay)
kiwi
  • 13
  • 4
  • 1
    Does this answer your question? [Add days to JavaScript Date](https://stackoverflow.com/questions/563406/add-days-to-javascript-date) – Sajeeb Ahamed May 09 '20 at 10:09
  • One more thing to mention you have misspelt the variable name `hundredthDay` in console.log. Sometimes our code will be correct but these typos can cause a headache so always check for typos. – MD M Nauman May 09 '20 at 10:14
  • @MDMNauman—and *birthday* is undefined… – RobG May 09 '20 at 11:32
  • Thank you for the quick comments. Appreciate it. I edited my post after your comments and also looked at the answer from @norbitrial :) – kiwi May 09 '20 at 12:43

1 Answers1

1

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() + 1 + 100));
console.log(result);

I hope this helps!

Community
  • 1
  • 1
norbitrial
  • 14,716
  • 7
  • 32
  • 59