You can simply use the setDate() method to add number of days to current date using JavaScript. Also note that, if the day value is outside of the range of date values for the month, setDate() will update the Date object accordingly (e.g. if you set 32 for August it becomes September 01).
Let's try out the following example to understand how it actually works:
// Get current date
var date = new Date();
// Add five days to current date
date.setDate(date.getDate() + 5);
console.log(date);
Similarly, you can also add number of days to a particular date in JavaScript.
// Specific date
var date = new Date('August 21, 2021 16:45:30');
// Add ten days to specified date
date.setDate(date.getDate() + 10);
console.log(date);
Use the
setDate()MethodYou can simply use the
setDate()method to add number of days to current date using JavaScript. Also note that, if the day value is outside of the range of date values for the month,setDate()will update the Date object accordingly (e.g. if you set 32 for August it becomes September 01).Let's try out the following example to understand how it actually works:
Similarly, you can also add number of days to a particular date in JavaScript.
need an explanation for this answer? contact us directly to get an explanation for this answer