Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    compare date strings

    Hey guys,

    I'm having a problem with JS dates!

    I have a two dates in this format

    YYYY-MM-DD

    and I need to compare them to see if one is in the future... eg:

    current today's date = 2005-01-05
    entered date = 2005-01-07

    The code should alert the user to a future date and throw an error.

    How can I do this? I tried using the new Date() but it didnt seem to work properly!!
    thanks for any help

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Split them up and use this Date constructor:

    new Date(yr_num, mo_num, day_num);

    Take note that mo_num is zero-based so you have to substract 1.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    as glenn says, simply compare
    var today = newDate()
    with
    var inputday = newDate(y,m,d)
    taking care that months number starts with 0;

    see an example:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script language="JavaScript" type="text/JavaScript">
    function 
    calculate(f){
    var 
    entry f.newD.value.split('/');//splits input value;
    if((entry.length<3)||(entry[0].length!=2)||(entry[1].length!=2)||(entry[2].length!=4)||(Number(entry[0]>31))||(Number(entry[1]>12))){//some limitative conditions
    alert('Plese input data in format dd/mm/yyyy !');
    f.difD.value '';
    return 
    false;
    }
    y=entry[2]*1;
    m=entry[1]*1-1;//sets months to default values i.e Jan = 0; Feb = 1; etc.
    d=entry[0]*1;
    var 
    newDate = new Date(y,m,d);//sets the new date
    var today = new Date();//sets the today date
    dif newDate-today;//difference in milliseconds
    dif Math.ceil(dif/1000/60/60/24);//difference in days
    if(dif>0){//if the date is in the future
    alert('The chosen date is in the future. Please input a valid date!');
    f.difD.value '';
    return 
    false;
    }
    f.difD.value dif ' days';//assign the difference
    }
    </script>
    </head>
    <body>
    <form>
      <input name="newD" type="text">
      Enter the date in format dd/mm/yyyy<br>
      <input type="button" onclick="return calculate(this.form)" value="Calculate">
      <br>
    <input name="difD" type="text"> difference in days
    </form>
    </body>
    </html> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    thanks guys - it's done the trick perfectly


  •  

    Posting Permissions

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