SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to add days to the now date ?

    Code:
    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

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by runeveryday View Post
    if i want to add 1 day 10 hours 50 minute 30 second to the now time, how to wirte the endtime code?
    Here's how you can do it.

    Code javascript:
    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:

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

    That function might look like this:

    Code javascript:
    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.

    Code javascript:
    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.

    Code javascript:
    ...
    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:

    Code javascript:
    ...
    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:

    Quote Originally Posted by runeveryday View Post
    why there is no value of the endtime?
    That's because you had a syntax error in the code.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Here's how you can do it.

    Code javascript:
    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:

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

    That function might look like this:

    Code javascript:
    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.

    Code javascript:
    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.

    Code javascript:
    ...
    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:

    Code javascript:
    ...
    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.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by runeveryday View Post
    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
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •