Get Number of Days Between Two Dates in Javascript

by Sasikumar 2014-06-18 09:48:29

To get number of days between two dates in javascript use the following function,
Javascript Function:
function days_between(date1, date2)
 {
    var oneday_time = 1000 * 60 * 60 * 24;

    // Convert dates to milliseconds
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();

    // Calculate the difference
    var diff_ms = Math.abs(date1_ms - date2_ms);
    
    // Convert back to days and return
    return Math.round(diff_ms / oneday_time);
}
Usage Example:
var days = days_between(start_date, end_date);
alert(days);
"days" will hold the no of days between start_date and end_date.
 
1302
like
0
dislike
0
mail
flag

You must LOGIN to add comments