Tuesday, 12 July 2011

javascript date difference‏

  function DateDiff() {
       
           var sFromdate = document.getElementById("<%=txtDateFrom.ClientID%>")
            var sTodate = document.getElementById("<%=txtDateTo.ClientID%>")

            if (!validateDates(txtDateFrom, txtDateTo)) {
                alert("From Date should greater then To Date");
                document.getElementById("<%=txtDateTo.ClientID%>").value = '';
                document.getElementById("<%=txtDateTo.ClientID%>").focus();
                return false;
            }

            var Fromdate = new Date(sFromdate.value)//.toDateString();
            var Todate = new Date(sTodate.value)//.toDateString();

            // The number of milliseconds in one day
            var ONE_DAY = 1000 * 60 * 60 * 24

            // Convert both dates to milliseconds
            var date1_ms = Fromdate.getTime()
            var date2_ms = Todate.getTime()

            // Calculate the difference in milliseconds
            var difference_ms = Math.abs(date1_ms - date2_ms)

            // Convert back to days and return
            if (Math.round(difference_ms / ONE_DAY) > 10) {

                alert("Date difference should not be greater then 10 Days")
                document.getElementById("<%=txtDateTo.ClientID%>").value = '';
                document.getElementById("<%=txtDateTo.ClientID%>").focus();
                return false;
            }
        }  

No comments:

Post a Comment