compairing dates like 08/29/2007 to 08/30/2007

Discussion in 'JavaScript' started by selfAfflicted, Aug 29, 2007.

  1. #1
    is there a way to compare dates/times using javascript that are already coded to output like this: 08/29/2007 08:30 to 08/30/2007 10:00

    Var CurrentDateTime output: "MM/DD/YYYY HH:MM"

    I am trying to find a way to calculate if the deadline for a project has passed, and have been stuck on the if statement, since you cant really compare variables that have slashes, spaces, and colons in them... or can you?

    Any help would be appreciated.
    thx
     
    selfAfflicted, Aug 29, 2007 IP
  2. selfAfflicted

    selfAfflicted Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I guess my original question was if you could do math with a variable that output like this "08/29/2007 9:30" I am going to go back to that. Is there a way, if I have no control over how the variable is populated if I can edit its contents.

    such as:
    If I have a variable named currentDateTime and its output is "MM/DD/YYYY HH:MM" is there a way to separate out its values like this: "MM" = currentMonth "DD" = currentDay "YYYY" = currentYear "HH" = currentHour "MM" = currentMinute.

    The reason I dont have control over the currentDateTime script is because its a built in function, that calls for the server time.
     
    selfAfflicted, Aug 29, 2007 IP
  3. ssanders82

    ssanders82 Peon

    Messages:
    77
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No, you can't do math on that directly (to my knowledge). You will have to split out the month, date, year, etc.

    I found this page: http://www.mattkruse.com/javascript/date/

    Click "source" at the top and look at the getDateFromFormat function.

    Or, you could also split the string yourself. Off the top of my head:

    // Split by the space
    var arr = CurrentDateTime.split(" ");
    // arr[0] now has MM/DD/YYYY and arr[1] has HH:MM
    var arr2 = arr[0].split("/");
    // arr2[0] is MM, arr2[1] is DD and arr2[2] is YYYY
    arr3 = arr[1].split(":");
    // arr3[0] is HH and arr3[1] is MM
    
    Code (markup):

    Then you can create a javascript date object

    Look at the constructor for new Date(dateString)

    http://www.comptechdoc.org/independent/web/cgi/javamanual/javadate.html

    I don't know if that all made sense, let me know if you need more help.
     
    ssanders82, Aug 29, 2007 IP
    selfAfflicted likes this.
  4. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #4
    m0nkeymafia, Aug 29, 2007 IP
    selfAfflicted likes this.
  5. Awanish

    Awanish Peon

    Messages:
    53
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    But I think the way ssanders82 suggested, provides you good control. I would prefer to go that one, as I was also facing the same problem, let me send you once it is done.
     
    Awanish, Sep 4, 2007 IP