In this article, we will see some of the efficient ways to get tomorrow date using javascript.
Knowing how to play with date is important in javascript application development. because it is one of the common scenarios you will face in application development.
Recent Article
How to Integrate Google sheet in nodejs Application
Kubernetes for Nodejs developers
Tomorrow date in javascript
Let's see some of the easy ways to get tomorrow's date using javascript and some external libraries in javascript
Built-in Method
Here, we are going to use a built-in Date
method to get tomorrow's date in javascript.
new Date()
returns the current date in javascript. Also, the getDate
method in javascript returns the current date value(1-31).
we are going to increment that value and use function setDate
to get tomorrow's date.
consttoday=newDate()// to return the date number(1-31) for the specified dateconsole.log("today => ",today)lettomorrow=newDate()tomorrow.setDate(today.getDate()+1)//returns the tomorrow dateconsole.log("tomorrow => ",tomorrow)
Image may be NSFW.
Clik here to view.
Momentjs
After that, we are going to use momentjs to get tomorrow date in javascript.
Let's install momentjs in our project using the command,
npm i moment
Now, you can get the current date using moment instance inside your project.
consttodayMoment=moment()//returns the current date with moment instance.
To get tomorrow date, you just need to add +1 days to the today
moment instance.
consttomorrowMoment=todayMoment.clone().add(1,'days')
Here, we clone the todayMoment
instance. because moment instance are mutable. it's always better the clone it before manipulating it.
Image may be NSFW.
Clik here to view.
Date Fns
Now, we will see how to get tomorrow date using date-fns. it is so simple to get the date using date-fns.
consttomorrowFns=add(newDate(),{days:1})console.log("tomorrow",tomorrowFns)
Using, we can add days
,month
and year
easily.
varresult=add(newDate(2020,8,1,10,19,50),{years:2,months:9,weeks:1,days:7,hours:5,minutes:9,seconds:30,})
Image may be NSFW.
Clik here to view.
Conclusion
We can use any one method depends upon the requirement. if you don't want to install a library, you can go with javascript in-built method to solve the problem.