I have some data that's inconsistent. It's all time related but I have some records that shows 3pm others 14:00.
Is there an easy way to normalize that in JS?
Thanks
I have some data that's inconsistent. It's all time related but I have some records that shows 3pm others 14:00.
Is there an easy way to normalize that in JS?
Thanks
This function will return you a 24-hour-formatted time
function normaliseTime(time) {
// If there is AM/PM in the string, do conversion
if (time.toUpperCase().indexOf('M') >= 0) {
// Remove the AM/PM text and split the hour and minute
var tmArray = time.replace(/\D/g, '').split(':');
// If PM, add 12 to the hour
if (time.toUpperCase().indexOf('PM') >= 0) {
tmArray[0] = parseInt(tmArray[0]) + 12;
}
// If minutes were not provided (i.e., 3PM), add 00 as minutes
if (tmArray.length < 2) {
tmArray[1] = '00';
}
return tmArray.join(':');
}
// If there was no AM/PM in the input, return it as is
return time;
}