-2

const today = new Date()
const tomorrow = new Date(today)
const newDate = tomorrow.setDate(tomorrow.getDate() + 2)
console.log(newDate.toLocaleDateString('en-us'))

I'm trying to get next 2 days date with mm/dd/yyyy format, but getting issues. I tried with following code:

console.log(new Date().toLocaleDateString('en-US'));

Example: today's date >> 6/1/2022

Expected result : 6/3/2022

Rajasekhar
  • 2,215
  • 3
  • 23
  • 38
  • 2
    So you tried _nothing_, and that didn't work? It's not terribly surprising. – jonrsharpe Jun 01 '22 at 07:08
  • check this one https://stackoverflow.com/questions/23081158/javascript-get-date-of-the-next-day – K Khan Jun 01 '22 at 07:26
  • Does this answer your question? [JavaScript, get date of the next day](https://stackoverflow.com/questions/23081158/javascript-get-date-of-the-next-day) – jonrsharpe Jun 01 '22 at 07:28

1 Answers1

3

Your problem comes from the fact, .setDate does not return a Date Object, but instead modifies the Date Object you call it on.

This means, tomorrow will be modified by calling .setDate.

Just change your code to the following, to get the expected result:

const today = new Date()
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 2)
console.log("today:", today.toLocaleDateString('en-US'))
console.log("in two days:", tomorrow.toLocaleDateString('en-us'))
GottZ
  • 4,824
  • 1
  • 36
  • 46