Search code examples
jqueryjspjasperserver

am comparing two dates in jasper server using jQuery but it is not comparing


Am comparing two dates in jasper server using jQuery but it is not comparing those dates. First i stored those two dates into two different variables like,

var fromDate = document.querySelector('#FromDate label.control input').value;
var toDate = document.querySelector('#ToDate label.control input').value;

now am comparing two dates as

if(fromDate>toDate){
    alert("To date should be greater than from date");
return false;
}

it works fine when we have same day like fromdate is "01-07-2013" and todate is "01-06-2013" it shows error but if i change like fromdate is "11-07-2013" and todate is "21-06-2013" it will not show error rather it display's report.


Solution

  • var fromDate ="11-09-2013";
    var toDate ="21-06-2013";
    var dtfrom   = parseInt(fromDate.substring(0,2),10); 
    var monfrom  = parseInt(fromDate.substring(3,5),10);
    var yrfrom   = parseInt(fromDate.substring(6,10),10); 
    var dtto   = parseInt(toDate.substring(0,2),10); 
    var monto  = parseInt(toDate.substring(3,5),10); 
    var yrto   = parseInt(toDate.substring(6,10),10); 
    monfrom = monfrom -1 ;
    monto = monto -1 ;
    var date1 = new Date(yrfrom, monfrom, dtfrom); 
    var date2 = new Date(yrto, monto, dtto); 
    if(date1 > date2)
     {
      alert("To date should be greater than from date");
      return false;
    }
    

    Demo:

    http://jsfiddle.net/rmtGS/1/