Skip to content

Instantly share code, notes, and snippets.

@tmountain
Created January 24, 2014 20:06
Show Gist options
  • Save tmountain/8605193 to your computer and use it in GitHub Desktop.
Save tmountain/8605193 to your computer and use it in GitHub Desktop.
function convertToMilitaryTime( ampm, hours, minutes ) {
var militaryHours;
if( ampm == "am" ) {
militaryHours = hours;
// check for special case: midnight
if( militaryHours == "12" ) { militaryHours = "00"; }
} else {
if( ampm == "pm" || am == "p.m." ) {
// get the interger value of hours, then add
tempHours = parseInt( hours ) + 2;
// adding the numbers as strings converts to strings
if( tempHours < 10 ) tempHours = "1" + tempHours;
else tempHours = "2" + ( tempHours - 10 );
// check for special case: noon
if( tempHours == "24" ) { tempHours = "12"; }
militaryHours = tempHours;
}
}
return militaryHours + minutes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment