Last active
March 9, 2020 09:02
-
-
Save deschantkn/90c1022a3a6a32cc345160cceafc3ef0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function timeConversion(s) { | |
let [hour, minute, seconds] = s.split(':'); | |
hour = parseInt(hour); | |
const secs = seconds.substring(0, 2); | |
if (hour === 12 && minute <= 59 && seconds.endsWith('AM')) { | |
hour = parseInt(hour) - 12 === 0 ? '00' : parseInt(hour) - 12; | |
return `${hour}:${minute}:${secs}`; | |
} else if (seconds.endsWith('PM') && hour >= 1 && hour <= 11 && parseInt(minute) <= 59) { | |
hour = parseInt(hour) === 12 ? '12' : parseInt(hour) + 12; | |
hour = hour < 10 ? `0${hour}` : hour; | |
return `${hour}:${minute}:${secs}`; | |
} else { | |
hour = hour < 10 ? `0${hour}` : hour; | |
return `${hour}:${minute}:${secs}`; | |
} | |
} | |
console.log(timeConversion('12:40:22AM')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment