Skip to content

Instantly share code, notes, and snippets.

@eshacker
Created January 26, 2016 11:00
Show Gist options
  • Save eshacker/7e77a0a987b78da894cb to your computer and use it in GitHub Desktop.
Save eshacker/7e77a0a987b78da894cb to your computer and use it in GitHub Desktop.
Converting comman man time to military time
function convertPM(time){
if(time.startsWith('12')){
return time.replace('PM','');
}
return (parseInt(time.split(':'))+12) + time.substring(2, time.length-2);
}
function convertAM(time){
if(time.startsWith('12')){
return '00'+time.substring(2, time.length-2);
}
return time.substring(0, time.length-2);
}
function main() {
var time = getTime(); // "HH:MM:SSAM" or "HH:MM:SSPM"
if(time.indexOf('P')>0)
console.log(convertPM(time));
else
console.log(convertAM(time));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment