The Guy Who Keeps Coding While True Yavuz Ege Özcan's blog

Parsing Time Manually 8 years ago

How do you get the minute difference between two 12-hour dates encoded in a string like

"12:05am-02:55pm"

?

Short Answer

You shouldn’t. Get your data properly!

But Still…

function CountingMinutesI(str) {
var times = str.replace(";\"","").split("-").map(function(t) {
t = t.split(":");
var h = parseInt(t[0], 10);
var m = parseInt(t[1], 10);
var am = t[1].indexOf("am") > 0;
if(h === 12) h = 0;
var mins = m + h * 60;
if(!am) mins += 720;
return mins;
});
var diff = times[1] - times[0];
return diff > 0 ? diff : diff + 1440;
}

I hate myself.