Thursday, 22 September 2011

Two Date Differences in JQUERY



var startDate = $('#dateTextField').val();       //i.e 22/9/2011   
var d1 = ddmmyyyyToDate(startDate);        
var endDate = $('#dateTextFieldEndDate').val();         
var d2 = ddmmyyyyToDate(endDate);          //i.e 12/09/2011 
var diff = d2 - d1;                                          //i.e -864000000
alert(startDate +" "+endDate+" "+diff);         
Output: -ve value: End Date is less than Start Date.
              +ve : Success (  Greater than ZERO.)

/*------------------This Function converts Date object into String------------------*/
function ddmmyyyyToDate(str) {
    var parts = str.split("/");                  // Gives us ["dd", "mm", "yyyy"]
    return new Date(parseInt(parts[2], 10),      // Year
                    parseInt(parts[1], 10) - 1// Month (starts with 0)
                    parseInt(parts[0], 10));     // Day of month
}


No comments:

Post a Comment