how to convert string to time with format HH:MM AM/PM in JAVASCRIPT

2.9k views Asked by At

this will be my first time to use javascript as a part of my project. And i need to convert a string in format HH:mm AM/PM to a 24 hr format (00:00:00) time. Please help guys! Thank you in advance.

1

There are 1 answers

4
hsz On BEST ANSWER

Try with:

var input   = '10:23 PM',
    matches = input.toLowerCase().match(/(\d{1,2}):(\d{2}) ([ap]m)/),
    output  = (parseInt(matches[1]) + (matches[3] == 'pm' ? 12 : 0)) + ':' + matches[2] + ':00';

console.log(output); // 22:23:00