JavaScript Date Tomorrow: 3 Quick and Efficient Methods

Believe me, me too, I went through this quest before. Trying to find ways how to get tomorrow’s date in JavaScript. Dates manipulation in JavaScript might look quite confusing because of the variety of options JavaScript provides in this concern. Nevertheless, by the time you finish reading this quick definitive guide, you’ll be armed with the knowledge to effortlessly get tomorrow’s date in JavaScript with different approaches.

So, let’s dive in…

1. Using ‘setDate()’ Method

Let’s begin in the easiest straightforward way,

const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);

const tomorrowDateString = tomorrow.toDateString();
const tomorrowLocalFrDateString = tomorrow.toLocaleDateString("fr-FR");

console.log(tomorrowDateString);
console.log(tomorrowLocalFrDateString);

In this technique, we’re using the JavaScript date constructor to get today’s date (line 1). After that, we’re creating another instance of it and set the day of the date to tomorrow (line 3). Indeed, the setDate() method changes the day of the month for this date according to the local time.

In line 5 we’re formatting the date portion dat of the variable tomorrow into a date interpreted in the local timezone (of your machine) using the method toDateString().

In line 6, on the other hand, we’re specifying the region and language for making the date string through the first argument passed to the method toLocalDateString().

If we run this piece of code, we’ll get something like the following,

Wed Aug 09 2023
09/08/2023

As you might have noticed, the date is displayed in a numeric French format. Yet, you can still use the second parameter “options” of the method toLocalString() to adjust the output format like in,

const options = {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
};
const tomorrowLocalFrDateString = tomorrow.toLocaleDateString("fr-FR", options);

In this case, the output would look like the following,

jeudi 10 août 2023

That’s it for the first technique…Now, let’s move on to the second one,

2. Using Timestamp

We can also use the current date’s timestamp and add 24 hours’ worth of milliseconds to get tomorrow’s date in JavaScript. Here’s an example,

const todayTimestamp = Date.now(); // in milliseconds
const tomorrowTimestamp = todayTimestamp + 24 * 60 * 60 * 1000;

const today = new Date(todayTimestamp);
const tomorrow = new Date(tomorrowTimestamp);

const todayDateString = today.toDateString();
const tomorrowDateString = tomorrow.toDateString();

console.log("Today's date is: ", todayDateString);
console.log("Tomorrow's date is: ", tomorrowDateString);

If your run this piece of code on your end, you’ll get something similar to that ( with different dates for sure),

Today's date is:  Wed Aug 09 2023
Tomorrow's date is:  Thu Aug 10 2023

So, the only difference in this approach is that we’re dealing with timestamps explicitly through the use of Date.now().

Now, let’s see the last method in our list to find tomorrow’s date in JavaScript…

3. Using Moment.js

Moment.js is a JavaScript library that makes manipulating date/time a breeze. Here’s the proof,

const moment = require("moment");

const tomorrow = moment().add(1, "d");
console.log("Tomorrow's date is: ",tomorrow.format("MM/DD/YYYY"));

The output of running this code looks like the following,

Tomorrow's date is:  08/10/2023

Moment.js is an excellent and rich library, you can look through their documentation to learn more about it.

So that’s it…In the ever-evolving world of JavaScript, the ability to work with dates is a fundamental skill that can save you time and enhance the functionality of your projects. In this blog post, we explored three diverse methods to obtain tomorrow’s date. From the simplicity of the setDate() method to the precision of timestamps, and the flexibility of Moment.js, you now have a trio of techniques at your fingertips.

So, go ahead and give each of these methods a try in your own projects. Remember, as you navigate the ever-shifting landscape of programming, your ability to adapt and learn will keep you ahead of the curve. Happy coding, and may your journey with JavaScript be filled with endless discoveries!

Leave a Reply

Your email address will not be published. Required fields are marked *