Skip to content

Instantly share code, notes, and snippets.

View luischicllarosas's full-sized avatar
🏠
Working from home

punkkeke luischicllarosas

🏠
Working from home
View GitHub Profile
@apolopena
apolopena / JSConvert12to24Time.js
Last active May 2, 2023 12:24
JavaScript: convert 12 hours time string to 24 hour time string
// NOTE: time string must look like this 07:05:45PM or this 07:05:45AM and account for 12:00:00AM and convert 12:00:00pm to 12:00:00
function timeConversion(s) {
const ampm = s.slice(-2);
const hours = Number(s.slice(0, 2));
let time = s.slice(0, -2);
if (ampm === 'AM') {
if (hours === 12) { // 12am edge-case
return time.replace(s.slice(0, 2), '00');
}
return time;