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!
- How to Serve Static Files with Nginx and Docker
- How to deploy a .NET app using Docker
- Best Visual Studio Code Extensions for 2022
- How to build a Discord bot using TypeScript
- How to deploy a PHP app using Docker
- Getting Started with Deno
- How to deploy a MySQL Server using Docker
- Learn how to use v-model with a custom Vue component
- Using Puppeteer and Jest for End-to-End Testing
- Learn how to build a Slack Bot using Node.js
- Setting Up Stylus CSS Preprocessor
- Using Axios to Pull Data from a REST API