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:
const today = new Date() // get today's date
const tomorrow = new Date(today)
tomorrow.setDate(today.getDate() + 1) // Add 1 to today's date and set it to tomorrow
console.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:
const currentDate = new Date("2022-10-31")
const nextDate = new Date(currentDate)
nextDate.setDate(currentDate.getDate() + 1)
console.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:
const getNextDays = (currentDate = new Date(), daysToAdd = 1) => {
const nextDate = new Date(currentDate)
nextDate.setDate(currentDate.getDate() + daysToAdd)
return nextDate
}
Top comments (0)