How to add days to the now date?

var myDate = new Date();
var endtime= new Date(myDate.getDate()+1,23:59:59);
alert(endtime);

why there is no value of the endtime? if i want to add 1 day 10 hours 50 minute 30 second to the now time, how to wirte the endtime code? thank you

Here’s how you can do it.


var days = 1,
    hours = 10,
    minutes = 50,
    seconds = 30,
    myDate = new Date(),
    endtime;
endtime = new Date(myDate.setDate(myDate.getDate() + days));
endtime = new Date(endtime.setHours(endtime.getHours() + hours));
endtime = new Date(endtime.setMinutes(endtime.getMinutes() + minutes));
endtime = new Date(endtime.setSeconds(endtime.getSeconds() + seconds));
alert(endtime);

If you don’t like doing that each time, you can write a function for it, so that you just have to call:


var endtime = addToDate(new Date(), 0, 0, 1, 10, 50, 30);

That function might look like this:


function addToDate(thisDate, years, months, days, hours, minutes, seconds, milliseconds) {
    years = years || 0;
    months = months || 0;
    days = days || 0;
    hours = hours || 0;
    minutes = minutes || 0;
    seconds = seconds || 0;
    milliseconds = milliseconds || 0;

    var newDate = thisDate;

    newDate = new Date(newDate.setFullYear(newDate.getFullYear() + years));
    newDate = new Date(newDate.setMonth(newDate.getMonth() + months));
    newDate = new Date(newDate.setDate(newDate.getDate() + days));
    newDate = new Date(newDate.setHours(newDate.getHours() + hours));
    newDate = new Date(newDate.setMinutes(newDate.getMinutes() + minutes));
    newDate = new Date(newDate.setSeconds(newDate.getSeconds() + seconds));
    newDate = new Date(newDate.setMilliseconds(newDate.getMilliseconds() + milliseconds));
    return newDate;
}

Notice the repetition in there now? Let’s extract that out to a different function.


function addTypeToDate(date, type, value) {
    value = value || 0;
    
    var setType = 'set' + type,
        getType = 'get' + type;
    return new Date(date[setType](date[getType]() + value));
}
function addToDate(thisDate, years, months, days, hours, minutes, seconds, milliseconds) {
    var newDate = thisDate;

    newDate = addTypeToDate(newDate, 'FullYear', years);
    newDate = addTypeToDate(newDate, 'Month', months);
    newDate = addTypeToDate(newDate, 'Date', days);
    newDate = addTypeToDate(newDate, 'Hours', hours);
    newDate = addTypeToDate(newDate, 'Minutes', minutes);
    newDate = addTypeToDate(newDate, 'Seconds', seconds);
    newDate = addTypeToDate(newDate, 'Milliseconds', milliseconds);
    return newDate;
}

We can now easily remove that repetition, by using an array to store the values and loop through them.


...
function addToDate(thisDate, years, months, days, hours, minutes, seconds, milliseconds) {
    var types = [
        {type: 'FullYear', value: years},
        {type: 'Month', value: months},
        {type: 'Date', value: days},
        {type: 'Hours', value: hours},
        {type: 'Minutes', value: minutes},
        {type: 'Seconds', value: seconds},
        {type: 'Milliseconds', value: milliseconds},
    ],
        newDate = thisDate,
        i;

    for (i = 0; i < types.length; i += 1) {
        newDate = addTypeToDate(newDate, types[i].type, types[i].value);
    }
    
    return newDate;
}

Or, if you want to get fancy with some array handling methods, you could instead use:


...
function addToDate(thisDate, years, months, days, hours, minutes, seconds, milliseconds) {
    var types = [
        {type: 'FullYear', value: years},
        {type: 'Month', value: months},
        {type: 'Date', value: days},
        {type: 'Hours', value: hours},
        {type: 'Minutes', value: minutes},
        {type: 'Seconds', value: seconds},
        {type: 'Milliseconds', value: milliseconds},
    ];
    
    return types.reduce(function (newDate, typeInfo) {
        return addTypeToDate(newDate, typeInfo.type, typeInfo.value);
    }, thisDate);
}

But to answer the original question:

That’s because you had a syntax error in the code.

many many thanks, your code is poetry。 i can understand it well,i know the basic grammer of javascript. why i can’t write that.

Thanks. It’s something that takes time and experience. Half the battle is being able to recognise the good and bad parts, so that you can try to keep away from the bad ones :slight_smile:

I’ve been battling to write some code like this as well, but I have a form that has a date and an integer. The javascript should take the date and add the number of days (the integer) to it. I’m using HTML5 so I know it’s a valid date, but the setDate(getDate + days) returns really weird results.

Is there an easy way to debug javascript code ?

EDIT :: … my problem, I wasn’t using parseInt on the integer and it was treating it as text.