The following are the JavaScript code examples to increment a date by 1 day using the setDate()
and getDate()
methods.
The JavaScript method setDate()
sets the day of the date object.
Syntax
dateObj.setDate(dayValue)
Example 1. Increment Date by 1 Day in JavaScript
// Current date is 25th March 2020 var currDate = new Date(); console.log('Today the date is '+ currDate); // now increment it by 1 day currDate.setDate(currDate.getDate() + 1); console.log('Tomorrow the date is '+ currDate);
Output
Today the date is Wed Mar 25 2020 16:28:58 GMT+0530 (India Standard Time) Tomorrow the date is Thu Mar 26 2020 16:28:58 GMT+0530 (India Standard Time)
Example 2. Increment Date by 1 Day Using For-Loop
// Current date is 25th March 2020 var currDate = new Date(); // Increment for 2 days and print including today's date for (let i = 0; i < 3; i++) { console.log(currDate); currDate.setDate(currDate.getDate() + 1); }
Output
Wed Mar 25 2020 16:47:32 GMT+0530 (India Standard Time) Thu Mar 26 2020 16:47:32 GMT+0530 (India Standard Time) Fri Mar 27 2020 16:47:32 GMT+0530 (India Standard Time)
Example 3. Increment Date and Print in DD/MM/YYYY Format
// Current date is 25th March 2020 var currDate = new Date(); for (let i = 0; i < 3; i++) { var dd = currDate.getDate(); var mm = currDate.getMonth()+1; var yyyy = currDate.getFullYear(); if(dd<10) { dd='0'+dd; } if(mm<10) { mm='0'+mm; } var vDate = dd+'/'+mm+'/'+yyyy; console.log(vDate); currDate.setDate(currDate.getDate() + 1); }
Output
25/03/2020 26/03/2020 27/03/2020
Example 4. Increment Date by 1 Day and Print in MM/DD/YYYY Format
// Current date is 25th March 2020 var currDate = new Date(); for (let i = 0; i < 3; i++) { var dd = currDate.getDate(); var mm = currDate.getMonth()+1; var yyyy = currDate.getFullYear(); if(dd<10) { dd='0'+dd; } if(mm<10) { mm='0'+mm; } var vDate = mm+'/'+dd+'/'+yyyy; console.log(vDate); currDate.setDate(currDate.getDate() + 1); }
If you find the above code not well formatted, you can use JavaScript beautifier to beautify the JS code.
Output
03/25/2020 03/26/2020 03/27/2020
Reference:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate