Search code examples
jquerycalendarajaxcontroltoolkit

How to validate From and To dates in JQuery?



I have to validate two dates in my aspx page where I am using AjaxControlToolKit Calendar Extender.
Following is my Code:

function DateValidation() {
    alert($("#txtfromDup").val() + "\n" + $("#txttoDup").val())
    var fromDate = Date.parse($("#txtfromDup").val());
    var toDate = Date.parse($("#txttoDup").val());
    alert(fromDate + "\n" + toDate);
    var timeDiff = toDate - fromDate;
    var daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
    alert(daysDiff);
    if (daysDiff > 30) {
        alert('From Date shouldn\'t be less than To Date.');
        return false;
    } else
        return true;

}


Its not validating properly.
Someone please help me to resolve this.


Solution

  • I'd do something like

    var startdate = new Date( "2015-07-04");
    var enddate = new Date( "2015-08-11");
    var timeDiff = Math.abs(enddate.getTime() - startdate.getTime());
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
    alert(diffDays);
     //pseudo code:
      if (diffDays  > 30){
         //set timeFormat = "30days"
         alert('From Date shouldn\'t be less than To Date.');
        
     }else{
         alert('Valid.');  
     }