开发者

Need a javascript to compare dates [duplicate]

This question already has answers开发者_JS百科 here: Closed 11 years ago.

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜