1function tConvert (time) {
2 // Check correct time format and split into components
3 time = time.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];
4
5 if (time.length > 1) { // If time format correct
6 time = time.slice (1); // Remove full string match value
7 time[5] = +time[0] < 12 ? 'AM' : 'PM'; // Set AM/PM
8 time[0] = +time[0] % 12 || 12; // Adjust hours
9 }
10 return time.join (''); // return adjusted time or original string
11}
12
13tConvert ('18:00:00');
1var suffix = hour >= 12 ? "PM":"AM";
2var hours = ((hour + 11) % 12 + 1) + suffix