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:
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:
Print the date on the console:
Call the “getDate()” method to get the day from the date in the “setDate()” method and add “1” to it:
Finally, print the new date on the console using the “console.log()” method:
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:
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:
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:
Print the updated date on the console:
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.