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
CodePudding user response:
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'))