Lets see how to get tomorrow’s date in JavaScript or by using a open-source library like momentjs.
Read about : How to get Yesterday’s date in JavaScript
In JavaScript, to get tomorrow’s date we have to get the current date using new Date()
method and then we have to add one day to it and it will give us the date of the next day.
Using new date() to get tomorrow’s date in JavaScript
const today = new Date()
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 1)
const tomorrowDate = tomorrow.toDateString()
console.log(tomorrowDate)
Output:
Fri Jan 07 2022
Code Explanation:
new Date()
: Gives current date and time of the day.
getDate()
: It extract the date of the month from the string of new Date().
tomorrow.getDate() + 1
: We extract the date (Eg today is 6 ) and then add 1 to it, so we get 7 (next day date).
setDate()
: Allow us to change the day of the month. In the code above, we use setDate() to set the value to 7.
toDateString()
: It returns the date in proper format in English.
Using JavaScript Library – momentJs
Momentjs is an open-source JavaScript library which remove the need to use the native JavaScript new Date() method.
Using momentJs, we can get the current date in a proper date format by:
moment().format("YYYY-MM-DD")
So to get tomorrow’s date using momentJs we have to add 1 as days :
moment().add(1, "days").format("YYYY-MM-DD");
Related Topics: