Skip to content

Instantly share code, notes, and snippets.

@jinahadam
Created April 23, 2011 07:56
Show Gist options
  • Save jinahadam/938455 to your computer and use it in GitHub Desktop.
Save jinahadam/938455 to your computer and use it in GitHub Desktop.
timeBetweenTime
get_h: function(timeString) {
var seperator = timeString.indexOf(":");
return this.timeTrim(timeString.substring(0,seperator));
},
get_m: function(timeString) {
var seperator = timeString.indexOf(":");
var timeMinusHour = timeString.substring(seperator);
var theWhiteSpace = timeMinusHour.indexOf(" ");
return this.timeTrim(timeMinusHour.substring(1,theWhiteSpace));
},
get_am_pm: function(timeString) {
var theWhiteSpace = timeString.indexOf(" ");
var time = this.timeTrim(timeString.substring(theWhiteSpace));
if(time == "am") {
return 0
}
return 1;
},
timeTrim: function(timeString) {
var timeString = timeString.replace(/^\s\s*/, ''),
ws = /\s/,
i = timeString.length;
while (ws.test(timeString.charAt(--i)));
return timeString.slice(0, i + 1);
},
timeBetweenTimes: function(startime, endtime) {
//set up params
h1 = parseFloat(this.get_h(startime),10);
m1 = parseFloat(this.get_m(startime),10);
h2 = parseFloat(this.get_h(endtime),10);
m2 = parseFloat(this.get_m(endtime),10);
console.log(h1,m1,h2,m2);
var er3a = parseFloat(this.get_am_pm(startime));
var er3b = parseFloat(this.get_am_pm(endtime));
if ((er3a==0) && (h1==12)) {h1=0}
if ((er3a==0) && (er3b==1)) {
t1= (60*h1) + m1;
t2= ((h2+12) * 60) + m2;
t3= t2-t1;
t4= Math.floor(t3/60)
t5= t3-(t4*60)
}
else if ((er3a==1) && (er3b==0)) {
if (h2==12) {h2=0}
if (h1==12) {h1=0}
t1= (60*h1) + m1;
t2= ((h2+12) * 60) + m2;
t3= t2-t1;
t4= Math.floor(t3/60)
t5= t3-(t4*60)
}
else if ((er3a==0) && (er3b==0)) {
t1= (h1*60) + m1;
t2= (h2*60) + m2;
if (t2>t1) {
t3= t2-t1;
t4= Math.floor(t3/60)
t5= t3-(t4*60)
}
else {
t2= ((h2+24)*60) + m2;
t3= t2-t1;
t4= Math.floor(t3/60)
t5= t3-(t4*60)
}
}
else if ((er3a==1) && (er3b==1)) {
if (h1!=12) {h1=h1+12}
if (h2!=12) {h2=h2+12}
t1= (h1*60) + m1;
t2= (h2*60) + m2;
if (t2>t1) {
t3= t2-t1;
t4= Math.floor(t3/60)
t5= t3-(t4*60)
}
else {
t2= ((h2+24)*60) + m2;
t3= t2-t1;
t4= Math.floor(t3/60)
t5= t3-(t4*60)
}
}
return " " + t4 + " hrs" + " " + t5 + " mins";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment