Use the toLocaleString() method to change time formatting to 24 hours. The toLocaleString ()
method returns a string that represents the date and time.
Convert time in to US format.
const dateObject = new Date();
// in 24 hour format
console.log(
dateObject.toLocaleString('en-US', {
hour12: false,
}),
);
Convert AM/PM Time string to 24 Hours Format
let timeString = "01:02 PM";
let [fullMatch, time, modifier] = timeString.match(/(\d?\d:\d\d)\s*(\w{2})/i);
let [hours, minutes] = time.split(':');
if (hours === '12') {
hours = '00';
}
if (modifier === 'PM') {
hours = (parseInt(hours, 10) + 12).toString();
}
convertedTime = hours + ":" + minutes;
console.log(convertedTime);