Home >Web Front-end >JS Tutorial >How Do I Add One Day to a Date in JavaScript?
Adding One Day to Current Date in JavaScript
To increment a Date object by one day, JavaScript provides two key methods: getUTCDay() and setDate().
Incrementing Using 1:
You previously tried 1 on getUTCDay() and getUTCDate(), but this approach is incorrect. These methods return the current day and date, and adding one to them won't modify the date.
Correct Approach:
The correct approach is to use the setDate() method, which modifies the date portion of the Date object. Here's an example:
var date = new Date(); // add a day date.setDate(date.getDate() + 1);
By calling setDate() with the current date plus one, you effectively increment the date by one day. This method updates the date object's internal representation of time and ensures that it correctly displays the next day.
Example:
var date = new Date("March 8, 2023"); console.log(date); // "Wed Mar 08 2023 00:00:00 GMT+0000 (Coordinated Universal Time)" date.setDate(date.getDate() + 1); console.log(date); // "Thu Mar 09 2023 00:00:00 GMT+0000 (Coordinated Universal Time)"
The above is the detailed content of How Do I Add One Day to a Date in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!