am pm to 24 hours converter javascript

Solutions on MaxInterview for am pm to 24 hours converter javascript by the best coders in the world

showing results for - "am pm to 24 hours converter javascript"
Davida
22 Feb 2017
1function convertTime12To24(time) {
2    var hours   = Number(time.match(/^(\d+)/)[1]);
3    var minutes = Number(time.match(/:(\d+)/)[1]);
4    var AMPM    = time.match(/\s(.*)$/)[1];
5    if (AMPM === "PM" && hours < 12) hours = hours + 12;
6    if (AMPM === "AM" && hours === 12) hours = hours - 12;
7    var sHours   = hours.toString();
8    var sMinutes = minutes.toString();
9    if (hours < 10) sHours = "0" + sHours;
10    if (minutes < 10) sMinutes = "0" + sMinutes;
11    return (sHours + ":" + sMinutes);
12}