Last active
November 13, 2017 17:46
-
-
Save Swivelgames/c80e1e0dd5fcbef577331696df3f885c to your computer and use it in GitHub Desktop.
24 to 12 hour clock
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
/* golf */ | |
const toTwelveHour = time => { | |
const [hr,min] = time.split(':'); | |
return `${hr>12?hr-12:hr==0?12:hr}:${min}${hr>12?'PM':'AM'}`; | |
} | |
/* expanded utility */ | |
const toTwelveHour = (time) => { | |
const [hour, min] = time.split(':'); | |
const suffix = hour > 12 ? 'PM' : 'AM'; | |
const hr = hour > 12 ? hour - 12 : parseInt(hour, 10); | |
return `${hr === 0 ? 12 : hr}:${min}${suffix}`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment