How to get Tomorrow's Date in JavaScript
Table of Contents
Eventually, you will come across a situation where you'll need to get tomorrow's date.
Maybe you're working with databases or a calendar, and you need to know what the next day is.
Whatever the reason, in this post, we'll learn how to get tomorrow's date in JavaScript.
Getting Tomorrow's Date
Everything related to dates and times in JavaScript is based on the Date object.
The Date object is the native JavaScript representation of a date and time, which we are going to use to get tomorrow's date.
First, let's start off with the basics. Let's get the current date and time.
JAVASCRIPTconst now = new Date();
From here, we're going to take advantage of the Date object's getDate()
method, which returns the day of the month.
JAVASCRIPTconst now = new Date();
const today = now.getDate();
Now that we have today, we can just add one to it to get tomorrow's date. We will need to also use the setDate()
method to set our new date.
JAVASCRIPTconst now = new Date();
const today = now.getDate();
const tomorrow = new Date(now);
tomorrow.setDate(today + 1);
Just like that, tomorrow
is a new Date object with tomorrow's date. This is essentially the same as adding 24 hours to the time of now
. In other words, the time of day did not change, simply the day itself.
If you want the very beginning of tomorrow
, you can use the setHours()
method to set it all to 0
.
JAVASCRIPTconst now = new Date();
const today = now.getDate();
const tomorrow = new Date(now);
tomorrow.setDate(today + 1);
tomorrow.setHours(0, 0, 0, 0);
Now we have a new Date object with tomorrow's date at the very beginning of the day.
Conclusion
In this post, we took a look at how to get tomorrow's date in JavaScript. We learned how to get the current date and time, then add 24 hours to the time of day.
We even learned how to set it to the very beginning of tomorrow's day.
Hopefully, you've found this useful. Happy coding!
- Git Tutorial: Learn how to use Version Control
- How to Set Up Cron Jobs in Linux
- How to deploy a PHP app using Docker
- Getting Started with Deno
- How to deploy a MySQL Server using Docker
- How to deploy an Express app using Docker
- How to deploy a Node app using Docker
- Learn how to use v-model with a custom Vue component
- How to Scrape the Web using Node.js and Puppeteer
- Getting Started with Handlebars.js
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Learn how to build a Slack Bot using Node.js