I want to subtract the two different 24 hours time format.
I had tried with following :
var startingTimeValue = 04:40;
var endTimeValue = 00:55;
var hour = startingTimeValue.split(":");
var hour1 = endTimeValue.split(":");
var th = 1 * hour[0] - 1 * hour1[0];
var tm = 1 * hour[1] - 1 * hour1[1];
var time = th+":"+tm;
This code is working fine if second minutes is not greater than the first.but other case it will return minus values.
The above code sample values result :
time1 : 04:40
time2 : 00:55
The result should be : 03:45 (h:mi) format. But right now I am getting 04:-5 with minus value.
I had tried with the link as : subtract minutes from calculated time javascript but this is not working with 00:00 format. So how to calculate the result value and convert into hours and minutes?
I would try something like the following. The way I see it, it is always better to break it down to a common unit and then do simple math.
Try:
DEMO
This solution utilizes javascript built-in date. How it works:
time1, time2 is the number of miliseconds since 01/01/1970 00:00:00 UTC.
subtractedValue is the difference in miliseconds.
These lines reconstruct a date object to get hours and minutes.
This method may work for you:
Demo: http://jsfiddle.net/zRVSg/
This works better , A fiddle I just found
fiddle