-1

Possible Duplicate:
Compare 2 dates with JavaScript

I would like to have a Javascript function to compare 2 dates, the functionality should be as follows

If i given a date as 1-1-2011 and another as 31-12-2011 or 1-1-2011 it should prompt me a message such that date should be greater than the previous one.

Can any one help me

Community
  • 1
  • 1
Vivekh
  • 4,141
  • 11
  • 57
  • 102
  • 3
    You should at least SEARCH for this! http://stackoverflow.com/questions/492994/compare-2-dates-with-javascript – Erre Efe Jul 26 '11 at 11:06
  • 1
    What have you tried ? What error do you get ? What is your specific problem ? *(many people here are OK to help, but won't just give you the code : you need to show some efforts)* – Pascal MARTIN Jul 26 '11 at 11:06

1 Answers1

0

I will give you sample try as per your need

<script type="text/javascript">
    function CompareDates() {
        var str1 = document.getElementById('<%= txtDate.ClientID %>').value;
        var str2 = document.getElementById('<%= txtDate1.ClientID %>').value;
        var dt1 = parseInt(str1.substring(0, 2), 10);
        var mon1 = parseInt(str1.substring(3, 5), 10);
        var yr1 = parseInt(str1.substring(6, 10), 10);
        var dt2 = parseInt(str2.substring(0, 2), 10);
        var mon2 = parseInt(str2.substring(3, 5), 10);
        var yr2 = parseInt(str2.substring(6, 10), 10);
        var date1 = new Date(yr1, mon1, dt1);
        var date2 = new Date(yr2, mon2, dt2);
        if (date2 < date1) {
            alert("To date cannot be greater than from date");
            return false;
        }
    }
</script>

<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtDate1" runat="server" onBlur="CompareDates();"></asp:TextBox>
Developer
  • 8,390
  • 41
  • 129
  • 238