Do you want to find out what is the date tomorrow or the date after a given date? We will see how to do the same in this article.
JavaScript Date has 2 methods setDate()
and getDate()
. We can use them to find tomorrow's date:
1const today = new Date() // get today's date2const tomorrow = new Date(today)3tomorrow.setDate(today.getDate() + 1) // Add 1 to today's date and set it to tomorrow4console.log("Tomorrow is", tomorrow.toDateString()) // 👉 Tomorrow is Mon Nov 07 2022
Now the next question you will have is, will this work if today is the last day of the month or year?
Let's find out:
1const currentDate = new Date("2022-10-31")2const nextDate = new Date(currentDate)3nextDate.setDate(currentDate.getDate() + 1)4console.log("Next date is", nextDate.toDateString()) // 👉 Next date is Tue Nov 01 2021
The code will work even for 31st December.
The time will be set to the exact time as today. If you want to reset the time to midnight, you can use
tomorrow.setHours(0,0,0,0)
You can put the above code to a utility function to add a particular number of days to any date:
1const getNextDays = (currentDate = new Date(), daysToAdd = 1) => {2 const nextDate = new Date(currentDate)3 nextDate.setDate(currentDate.getDate() + daysToAdd)4 return nextDate5}
Do follow me on twitter where I post developer insights more often!
Leave a Comment