Skip to content

Instantly share code, notes, and snippets.

@Swivelgames
Last active November 13, 2017 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swivelgames/c80e1e0dd5fcbef577331696df3f885c to your computer and use it in GitHub Desktop.
Save Swivelgames/c80e1e0dd5fcbef577331696df3f885c to your computer and use it in GitHub Desktop.
24 to 12 hour clock
/* 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