Thursday, 19 February 2015

Compare Dates in javascript, jquery

Compare Dates  in javascript, jquery   

function dateVerity() {
          var from = document.getElementById('<%=Textbox1.ClientID%>').value;
          var to = document.getElementById('<%=Textbox2.ClientID%>').value;
          var arr_From = from.split("/");
          var arr_To = to.split("/");
         
          var d1 = new Date(arr_From[2], arr_From[1]-1, arr_From[0]); // Jan 1, 2011
          var d2 = new Date(arr_To[2], arr_To[1]-1, arr_To[0]); // Jan 2, 2011
          d1.setMonth(d1.getMonth() + 3); // Adding three months
          d1.setDate(d1.getDate() -1); // Substracting 1 day
          var d11 = d1.setHours(0, 0, 0, 0); // set hours to Zero because only dates will compare
          var d22 = d2.setHours(0, 0, 0, 0); // set hours to Zero because only dates will compare
       
          if (d22 < d11) {
              alert("There should be 3 months gap between from and to date");
              return false;
          }
          else {
              return true;
          }
         
      }