javascript - How to compute the difference between 2 time in jquery datetimepicker? -
i'm using jquery datetimepicker, want difference between start time , end time.
how can calculate using javascript/jquery code?
i set start , end time in html like.
<div class='input-group date' id='time_starts_at'> <input class="form-control" placeholder="time start's @ *" autocomplete="off" type="text" name="" id="calendar_starts_at_time"> <span class="input-group-addon"> <span class="glyphicon glyphicon-time"></span> </span> </div> <div class='input-group date' id='time_ends_at'> <input class="form-control" placeholder="time end's @ *" autocomplete="off" type="text" name="" id="calendar_ends_at_time"> <span class="input-group-addon"> <span class="glyphicon glyphicon-time"></span> </span> </div>
and, jquery codes getting value:
var timefrom = $start_time.data('date'); var timeto = $end_time.data('date');
curently, i'm doing manually splitting hour , minutes. adding validation on checking values before subtracting it.
is there best way ?
please help. thank you.
solved
thanks @felix kling, how calculate time difference between strings in javascript
function toseconds(time_str) { // extract hours, minutes , seconds var parts = time_str.split(':'); // compute , return total seconds return parts[0] * 3600 + // hour has 3600 seconds parts[1] * 60; // minute has 60 seconds } var = "10:55" //start time var b = "12:00" // end time var difference = math.abs(toseconds(a) - toseconds(b)); // format time difference var result = [ math.floor(difference / 3600), // hour has 3600 seconds math.floor((difference % 3600) / 60), // minute has 60 seconds difference % 60 ]; // 0 padding , concatation result = result.map(function(v) { return v < 10 ? '0' + v : v; }).join(':'); alert(result);