const time = '5:00AM';
function convertTo24HrsFormat(time) {
const slicedTime = time.split(/(PM|AM)/gm)[0];
let [hours, minutes] = slicedTime.split(':');
if (hours === '12') {
hours = '00';
}
let updateHourAndMin;
function addition(hoursOrMin) {
updateHourAndMin =
hoursOrMin.length < 2
? (hoursOrMin = `${0}${hoursOrMin}`)
: hoursOrMin;
return updateHourAndMin;
}
if (time.endsWith('PM')) {
hours = parseInt(hours, 10) + 12;
}
return `${addition(hours)}:${addition(minutes)}`;
}
console.log(`Converted time: ${convertTo24HrsFormat(time)}`);