July 30th, 2024
What's missing in this code?
// Convert AM/PM to 24-hour time!
convertTime('06:15:35PM'); // 18:15:35
convertTime('12:30:28AM'); // 00:30:28
convertTime('12:35:46PM'); // 12:35:46
function convertTime(time)
const [hour, minute, second] = time
.slice(0, -2)
.
(':');
const period = time.slice(-2);
let hour24 =
(hour);
if (period === 'PM' && hour24 !== 12)
hour24 +=
;
} else if (period === 'AM' && hour24 === 12)
hour24 =
;
}
const hourStr = hour24.toString().padStart(2, '0');
return `${hourStr}:${minute}:${second}`;
}
Type or select from these options