JavaScript

How to get tomorrow’s date in String format in JavaScript

Importing and assigning dates is an extremely significant characteristic of any web page. Its use can range from assigning dates of when a product is to be shipped to predicting the weather forecast of certain dates. The use of dates is not always limited to the current day and time but a user may require tomorrow’s date in many cases.

In this article, the necessary knowledge to import tomorrow’s date or even more days after that will be provided.

How do you obtain Tomorrow’s Date in a String Format?

The manner in which tomorrow’s date is calculated is very straightforward. This section will thoroughly explain how this function can be implemented using JavaScript.

Step 1: Using Date()
The first step of this process is to get today’s date as shown below:

var dateToday = new Date();
console.log("Today's date is: ",dateToday);

Date() is a constructor that lets you access various properties of a date. In this instance, the constructor is being used to access the current date and time from your system. That data is then stored into a variable named “dateToday”. Check out the example that has been provided below:

Step 2: Get and Set Date
The “setDate()” and “getDate()” functions are being utilized in this section to assign tomorrow’s date, as we see in the following code:

let dateTomorrow =  new Date();
dateTomorrow.setDate(dateToday.getDate() + 1);
console.log("Tomorrow's date is: ",dateTomorrow);

Firstly, another variable named “dateTomorrow” is declared. Next, the “setDate()” function is used to set the date in the variable. To assign tomorrow’s date, the “getDate()” method is used to access the date value from the “dateToday” variable and add 1 to it. This assigns tomorrow’s date to the “dateTomorrow” variable. Below is an example of this:

Step 3: Convert the variable to a String
The final step is to convert the variable “dateTomorrow” into a String. Below are two statements that you can use to achieve that depending on the format you prefer.

The “toDateString” format

The sample below demonstrates how to implement “toDateString()” function and what the output will look like:

dateTomorrow.toDateString();

The “toLocaleDateString” format

Below an example is given of how the “toLocaleDateString()” function works and it’s corresponding output:

dateTomorrow.toLocaleDateString();

Following these 3 simple steps will provide the desired output which in this case is tomorrow’s date.

Conclusion

The best way to calculate tomorrow’s date is to use the “getDate()” function which will return today’s date and then increment that value by 1 using the “setDate()” function. In this article, the methods and their functionality are explained in detail as well as how to format the date into various string formats.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.