www.webdeveloper.com
Results 1 to 6 of 6

Thread: Compare two dates

  1. #1
    Join Date
    May 2003
    Location
    India
    Posts
    275

    Compare two dates

    Hi

    How can I compare two dates (text box values ie, from dt & to Date) using javascript.

    Here is the script which I have used to compare two dates which I feel is not very feasibel.

    function dateCompare(dt1, dt2, cond)
    {
    var dt1String, dt1_dt, dt1_date, dt1_month, dt1_year;
    var dt2String, dt2_dt, dt2_date, dt2_month, dt2_year;

    dt1String = dt1;
    dt1_dt = new Date(dt1String.replace(/[-]/g,""));
    dt1_date = dt1_dt.getDate();
    dt1_month = dt1_dt.getMonth()+1;
    dt1_year = dt1_dt.getYear();

    dt2String = dt2;
    dt2_dt = new Date(dt2String.replace(/[-]/g,""));
    dt2_date = dt2_dt.getDate();
    dt2_month = dt2_dt.getMonth()+1;
    dt2_year = dt2_dt.getYear();

    if (cond == 1)
    {
    if (dt1_month==dt2_month)
    {
    if (dt1_date>dt2_date)
    {
    alert("Error Date Field...From date should not be Greater than To Date");
    return false;
    }

    if (dt1_year > dt2_year)
    {
    alert("Error Date Field...From Year should not be Greater than To Year");
    return false;
    }
    }
    else if (dt1_month > dt2_month)
    {
    if (dt1_year == dt2_year)
    {
    alert("Error Date Field...From Month should not be Greater than To Month");
    return false;
    }
    }
    else if (dt1_year > dt2_year)
    {
    alert("Error Date Field...From Year should not be Greater than Current Year");
    return false;
    }
    } //cond==1
    return true;
    }

    Here the parameter cond is for Greater Than or Less than (if cond=1 then Greater than if 2 than Less than).

    Urgent please help.

    shara

  2. #2
    Join Date
    Nov 2003
    Location
    Aachen, Germany
    Posts
    4,090
    Hi!

    I am not 100% sure of what you need, but from your code I deduct, that the to date has to be at least 1 day later than the from date. If so, please try this:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <
    html>
    <
    head>
    <
    title>Untitled</title>
    <
    script language="JavaScript" type="text/javascript">
    <!--
    function 
    dateCompare(dt1dt2){
    var 
    dt1_dt Date.parse(dt1.value);
    var 
    dt2_dt Date.parse(dt2.value);
    if (!
    dt1_dt){
    alert('The entry in the from field is not a valid date');
    dt1.focus();
    return 
    false
    }
    else if (!
    dt2_dt){
    alert('The entry in the to field is not a valid date');
    dt2.focus();
    return 
    false
    }
    else if (
    dt1_dt >= dt2_dt) {
    alert("To date should be greater than From date."); 
    dt2.focus();
    return 
    false
    }
    else{
    alert('The difference between the two dates you entered is: '+(dt2_dt-dt1_dt)/86400000+' day(s).');
    return 
    true;
    }
    }
    //-->
    </script>
    </head>
    <body>
    <form>
    from: <input type="text" name="date1"><br>
    to: <input type="text" name="date2"><br>
    <input type="button" onClick="dateCompare(document.forms[0].date1,document.forms[0].date2)" value="Compare"><br>
    </form>
    </body>
    </html> 
    Cheers - Pit

  3. #3
    Join Date
    May 2003
    Location
    India
    Posts
    275
    Hi
    Exactly !!
    I have maintained 'dd-mmm-yyyy' date format for my project. When I use ur code to compare dates

    var dt1_dt = Date.parse(dt1);
    var dt2_dt = Date.parse(dt2);
    are taking NaN values. Why??

    To the date function I am just sending the text field value itself.

    shara

  4. #4
    Join Date
    Nov 2003
    Location
    Aachen, Germany
    Posts
    4,090
    Hi!

    Using the code I provided, the date format entered should be like:

    'mm-dd-yyyy', 'mm/dd/yyyy', 'mmm-dd, yyyy', 'mmm/dd, yyyy'...

    If you use mmm, the day has to follow and to be followed by a comma as a separtor before the year

    Cheers - Pit

  5. #5
    Join Date
    May 2003
    Location
    India
    Posts
    275
    How can I format 'mmm/dd/yyyy' in to 'mmm/dd, yyyy' format in Js

    shara

  6. #6
    Join Date
    Nov 2003
    Location
    Aachen, Germany
    Posts
    4,090
    Hi!

    I would suggest to work with dropdowns to select days, months and years for the two dates instead of text inputs. That would also avoid user errors (February 30th etc.).

    If you like, I can provide a code for that...

    Cheers - Pit

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles