I want to calculate duration using start time and end time.
I have a code that gets me the hours, but I need both hours and minutes also.
What changes should I make to the code?
function timeDiff( first, second ) {
var f = first.split(' '), s = second.split(' ');
if( first == '12 AM' ) f[0] = '0';
if( first == '12 PM' ) f[1] = 'AM';
if( second == '12 AM' ) s[0] = '24';
if( second == '12 PM' ) s[1] = 'AM';
f[0] = parseInt( f[0], 10 ) + (f[1] == 'PM' ? 12 : 0);
s[0] = parseInt( s[0], 10 ) + (s[1] == 'PM' ? 12 : 0);
return s[0] - f[0];
}
var FirstTime = '1 AM';
var SecondTime = '12 pM';
alert( timeDiff( FirstTime, SecondTime ) );