Skip to content

Instantly share code, notes, and snippets.

@deschantkn
Last active March 9, 2020 09:02
Show Gist options
  • Save deschantkn/90c1022a3a6a32cc345160cceafc3ef0 to your computer and use it in GitHub Desktop.
Save deschantkn/90c1022a3a6a32cc345160cceafc3ef0 to your computer and use it in GitHub Desktop.
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