Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Calculate Day Difference in 2 date textboxes

I have 2 asp.net texboxes with calendar extender. I want to find out the number of days between both dates when one of the date control is changed. how can I achieve this using jquery or javascript?

like image 217
Aneef Avatar asked Apr 09 '10 17:04

Aneef


People also ask

How can calculate number of days between two dates in JQuery?

// Determine the number of days between two dates. var days = millisBetween / (1000 * 3600 * 24);

How can calculate hours in JQuery?

Convert minutes to hours/minutes with the help of JQuery. // getting the hours. let hrs = Math. floor(mins / 60);


1 Answers

This should do the trick

var start = $('#start_date').val(); var end = $('#end_date').val();  // end - start returns difference in milliseconds  var diff = new Date(end - start);  // get days var days = diff/1000/60/60/24; 

Example

var start = new Date("2010-04-01"),     end   = new Date(),     diff  = new Date(end - start),     days  = diff/1000/60/60/24;  days; //=> 8.525845775462964 
like image 115
maček Avatar answered Sep 21 '22 19:09

maček