3 Efficient ways to get tomorrow date using javascript

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 way 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.

1const today = new Date()
2
3// to return the date number(1-31) for the specified date
4console.log("today => ",today)
5
6let tomorrow = new Date()
7tomorrow.setDate(today.getDate() + 1)
8//returns the tomorrow date
9console.log("tomorrow => ",tomorrow)

tomorrow date in javascript

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,

1npm i moment

Now, you can get the current date using moment instance inside your project.

1const 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.

1const 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.

tomorrow date

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.

1const tomorrowFns = add(new Date(),{
2 days: 1
3})
4
5console.log("tomorrow",tomorrowFns)

Using, we can add days,month and year easily.

1var result = add(new Date(2020, 8, 1, 10, 19, 50), {
2 years: 2,
3 months: 9,
4 weeks: 1,
5 days: 7,
6 hours: 5,
7 minutes: 9,
8 seconds: 30,
9})

tomorrow date

Conclusion

We can use any one method depends upon the requirement. if you don't want to install an library, you can go with javascript in-built method to solve the problem.

To Read More

Everything you need to know about d...

Docker volume is a persistent data storage mechanism to store the data in docker...

How to remove docker image - Docker...

This guide will cover everything you need to know about removing docker images f...

How to build an Actionable data ta...

In this article, we will see how to build an Actionable data table using a react...