#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    6
    Rep Power
    0

    comparing dates via javascript


    Ok, onChange I need it to check if one date is greater than 60 days of the current date.

    Code, using a little Cold Fusion:
    <CFSET Date60 = #now()#+60>
    <SCRIPT LANGUAGE="JAVASCRIPT">
    <cfoutput>
    function validateForm() {
    var ActivityDate2 = Date(myform.ActivityDate.value);
    var ActivityDate3 = Date(#Date60#);
    alert("Date2:" + ActivityDate2 + " " + "Date3:" + ActivityDate3);
    if (ActivityDate2 > ActivityDate3) {
    alert("Date can be no more than 60 days from now.");
    myform.ActivityDate.focus();
    return false;
    }
    return true;
    }
    </cfoutput>
    </script>

    Main problem right now being that ActivityDate3 does not return a date of now+60, just returns current date. Ultimately I would like it to only return true if the form date is within 60 days of the current date.

    Thanks.
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    To get a date 60 days from the current, in javascript:

    today = new Date()
    offset = 60
    newDay = today.getDate() + offset
    newDate = new Date (today.getYear(), today.getMonth(), newDay)

    It will increment, so that months/years are added if necessary.
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Posts
    804
    Rep Power
    100
    I like my way better:

    Code:
    var newDate = new Date((new Date())/1 + 60*24*60*60*1000);
    His way works too. But in JavaScript, dates actually have a time as well, and his way will give 0:00:00 as the time. Mine truly increments the date by exactly 60 days by adding 60 days * 24 hours * 60 minutes * 60 seconds * 1000 microseconds.
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    True, overlooked the time while in a hurry and just copying/pasting a solution.

    The reason for doing it the 'offset' way is simply because it is not so hardcoded as having the one liner you gave. In other words you can keep it in a javascript library as a fuction:

    function getDateOffset(value) {
    today = new Date()
    return new Date (today.getYear(), today.getMonth(), today.getDate() + value, today.getYear(), today.getMonth(), today.getSeconds())
    }

    That way you can stick the function in and use it like:

    var sixtyDaysTime = getDateOffset(60)

    or similar in your code, reusable code is a virtue!

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    Portability is the key factor, but I dislike all the get methods called in your example, Binky. I think it's easier to work with the epoch millisecond values, like clone did.
    Code:
    function getOffsetDate(days) {
      var now = new Date();
      return new Date(now.valueOf() + days * 86400000);
    }
    I dislike the divide by one shorthand, so I just called the valueOf method explicitly...
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Posts
    804
    Rep Power
    100
    Lol... I was gonna say the same thing Eggertson did. Incidentally I didn't even know about the valueOf() method but I tend to make things cryptic and/or as short as possible so I think I will keep dividing by one

IMN logo majestic logo threadwatch logo seochat tools logo