2012/01/30

Comapare date in Javascript

function CompareDates() {
            var fromDate = document.getElementById("<%=txtDateFrom.ClientID %>").value;
            var toDate = document.getElementById("<%=txtDateTo.ClientID %>").value;
            var dt1 = parseInt(fromDate.substring(0, 2), 10);
            var mon1 = parseInt(fromDate.substring(3, 5), 10);
            var yr1 = parseInt(fromDate.substring(6, 10), 10);
            var dt2 = parseInt(toDate.substring(0, 2), 10);
            var mon2 = parseInt(toDate.substring(3, 5), 10);
            var yr2 = parseInt(toDate.substring(6, 10), 10);
            var date1 = new Date(yr1, mon1, dt1);
            var date2 = new Date(yr2, mon2, dt2);
            if (date2 < date1) {
                alert("From date cannot be greater than to date ");
                return false;
            }
        }