Increase days by 5

Hi,

I have the following code which displays a date in an input field. What I would like to do is to have the day increase by 5 days. However, if the day is 30/31, it increases to 35/36 instead of rolling into the next month. How can I do this?

var future = new Date();
var fdd = future.getDate()+5;
var fmm = future.getMonth()+1; //January is 0!
var fyyyy = future.getFullYear();

if(fdd<10) {
    fdd='0'+fdd
} 

if(fmm<10) {
    fmm='0'+fmm
} 

future = fdd+'/'+fmm+'/'+fyyyy;
document.write(future);


document.getElementById("end").value = future;

Thanks

If you add too many days to the Date object, it will automatically roll over the month (or even the year) for you.

date = new Date("May 29, 2016");
// Sun May 29 2016 00:00:00 GMT+1200 (New Zealand Standard Time)
date.setDate(date.getDate() + 5);
// 1464868800000
date.getDate();
// 3
date;
// Fri Jun 03 2016 00:00:00 GMT+1200 (New Zealand Standard Time)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.