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.
const today = new Date()
// to return the date number(1-31) for the specified date
console.log("today => ",today)
let tomorrow = new Date()
tomorrow.setDate(today.getDate() + 1)
//returns the tomorrow date
console.log("tomorrow => ",tomorrow)
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.
const todayMoment = moment() //returns the current date with moment instance.
To get tomorrow date, you just need to add +1 days to the today
moment instance.
const tomorrowMoment = 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.
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.
const tomorrowFns = add(new Date(),{
days: 1
})
console.log("tomorrow",tomorrowFns)
Using, we can add days
,month
and year
easily.
var result = add(new Date(2020, 8, 1, 10, 19, 50), {
years: 2,
months: 9,
weeks: 1,
days: 7,
hours: 5,
minutes: 9,
seconds: 30,
})
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.
Top comments (1)
Hi,
You should avoid using Moment JS it is huge...
Date-fns or other libraries have maintenance costs if there is (major) version change
Vanilla JS getDate and setDate would suffice and you can vary the days, forward or back