DEV Community

Cover image for How to get tomorrow's date in JavaScript?
collegewap
collegewap

Posted on • Originally published at codingdeft.com

How to get tomorrow's date in JavaScript?

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)