I want to find difference between two time-components, but I am not getting what I want!
I have 2 drop-down list components in a form,
here is my code:
input set 1:
timefrm_ 4 -> 18:00
timeto_4 --> 23: 00
input set 2 :
timefrm_ 4 -> 23:00
timeto_4 --> 01 : 00
JavaScript code:
function time_diif4()
{
var timespan4from = HMStoSec1(timefrm_4);
var timespan4to = HMStoSec1(timeto_4);
var timespan_4 = timespan4to - timespan4from ;
var diff4 = convertMinutes(timespan_4);
alert(timespan4from);
alert(timespan4to);
alert(timespan_4);
alert(diff4);
}
var secondsPerMinute = 60;
var minutesPerHour = 60;
function HMStoSec1(T)
{
// h:m:s
var A = T.split(/\D+/) ;
return ((A[0]*60 + +A[1])*60 )
}
function convertMinutes(intSeconds)
{
return Math.floor(intSeconds/secondsPerMinute);
}
my output (as alert) is:
for input set 1:
64800 (in seconds)
82800 (in seconds)
18000 (in seconds)
300 (in minutes) — 5 hours ( 18:00
to 23:00)
for input set 2:
82800
5400
-77400 ( error it should be next day)
-1290
just find the remaining value in division to a day minutes.
when the start time is greater than finish time then it means that finish time belongs to next day. this code will solver this problem. but if you don’t want this to happen, you should prevent user to selecting this data.