Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get next two days date (day after tomorrow's date) a value using JavaScript

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading