I got a online challenge as below:
Given a time in -hour AM/PM format, convert it to military (24-hour) time.
Note: – 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.
Example
- Input ’12:01:00′.
- Return ’00:01:00′.
here is my solution:
function timeConversion(s) {
const validtime = s.replace(/.{2}$/, ' ' +s.substring(s.length-2));
const newDate = new Date(`02/06/2024 ${validtime }`);
return newDate.toLocaleTimeString('en-US', { hour12: false });
}
const result = timeConversion('12:30:59AM');
console.log("result", result);
But when I post this answer some of the conversion is failing. I am looking a help as that, what have I missed to consider here or what will be the issue with my solution?
1