1. Get today’s date
As we mentioned, we will get today’s date using the date object as shown below in the code. We will use the
toDateString() method to convert the date into a readable string.
let dt = new Date(); console.log(dt); // Wed Apr 06 2022 00:40:18 GMT+0530 (India Standard Time) dt = dt.toDateString(); console.log(dt); // Wed Apr 06 2022
2. Get tomorrow’s date
To calculate tomorrow’s date, first we have to get the current date (today) then add 1 day in the current date.
let today = new Date(); let tomorrow = new Date(today); tomorrow.setDate(tomorrow.getDate() + 1); console.log(tomorrow.toDateString()); // Thu Apr 07 2022
Here, we have used the
getDate() function to get the day of the date. After adding one day in the date, use the
setDate() function to set tomorrow’s date into the date object.
3. Get yesterday’s date
Same way, we can easily calculate the yesterday’s date. Here, we will subtract 1 day from the date.
let today = new Date(); let yesterday = new Date(today); yesterday.setDate(yesterday.getDate() - 1); console.log(yesterday.toDateString()); // Tue Apr 05 2022
I hope you find this article is helpful.
Thank you for reading. Happy Coding..!! 🙂