JavaScript

Add 1 Day to a Date Using JavaScript

JavaScript offers a Date object to perform various manipulations using date and time. However, sometimes a programmer may need to add a day to the current or the specified date. For this purpose, JavaScript provides a number of methods, including methods for adding days, months, or years to a specified or current date.

This article will demonstrate the methods to add a day to date using JavaScript.

How to Add 1 Day to a Date Using JavaScript?

For adding 1 day to a Date, use the given predefined JavaScript methods:

Let’s see how these methods will add a day-to-date.

Method 1: Add 1 Day to a Date Using setDate() Method

To add 1 day to a Date, use the “setDate()” method with the “getDate()” method. The getDate() method gets the day from the specified date, and the setDate() method sets the day of the month to the next day by adding 1 to the existing date.

Syntax
Follow the given syntax for adding 1 day in a Date using the setDate() method:

date.setDate(date.getDate() + 1);

Example 1: Add 1 Day to a Current Date
In the following example, first create a new Date object using the Date() constructor, which comprises the current date:

const date = new Date();

Print the date on the console:

console.log("Today: " + date);

Call the “getDate()” method to get the day from the date in the “setDate()” method and add “1” to it:

date.setDate(date.getDate() + 1);

Finally, print the new date on the console using the “console.log()” method:

console.log("Add 1 day: " + date);

The output shows that 1 day is successfully added to the current date:

Example 2: Add 1 Day to a Specified Date
Create a Date object by passing date any date, such as “23 June 2022” as an argument, and then execute the same code block:

const date = new Date("23 June 2022");

The output gives the “24 June 2022” date by adding “1” day in the specified date:

Method 2: Add 1 Day to a Date Using Date.now() Method

Another method for adding a day to a Date is the “now()” method of the Date object. It gives time in milliseconds. For adding a day, you have to add the milliseconds to the current date.

Syntax
Use the given syntax to add a day to Date using the Date.now() method:

new Date(Date.now() + (3600 * 1000 * 24))

Here, the Date.now() method gives the milliseconds of the current date and adds the milliseconds of a day in it to get the updated date.

Example
Create a new date object by passing the “Date.now()” method by adding milliseconds of a day as an argument:

const add1 = new Date(Date.now() + (3600 * 1000 * 24));

Print the updated date on the console:

console.log("Add 1 day: " + add1);

The output indicates that the date is now updated:

We have compiled all the methods to add 1 day in a date using JavaScript.

Conclusion

To add 1 day to date, use the pre-built JavaScript “setDate()” with the “getDate()” method and “Date.now()” method of the Date object. The getDate() method gets the day from the given date, and the setDate() method sets the day of the month to the next day by adding 1 to an existing day. Whereas the Date.now() method gives time in milliseconds. This article demonstrated the methods to add a day to date using JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.