需要一个JavaScript来比较日期

时间:2011-07-26 11:04:50

标签: javascript asp.net

  

可能重复:
  Compare 2 dates with JavaScript

我想有一个Javascript函数来比较2个日期,功能应该如下

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

任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

我会根据您的需要为您提供样品试用

<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>