This converter switches clock times between the 12-hour AM/PM format common in the United States and the 24-hour format used in most of the world and in any context where ambiguity is unacceptable. It works in both directions and gets the tricky midnight and noon cases right.
How it works
Converting from 12-hour to 24-hour applies two rules to the hour, leaving the minutes untouched:
if period is AM and hour == 12 -> hour = 0
if period is PM and hour != 12 -> hour = hour + 12
Converting from 24-hour back to 12-hour reverses this: the period is AM when the
hour is below 12 and PM otherwise, and the displayed hour is hour mod 12,
shown as 12 when that result is zero. The minute field is always two digits.
Example and tips
2:34 PM becomes 14:34, while 00:15 becomes 12:15 AM. The two cases worth
memorising are the boundaries: midnight is 12:00 AM equal to 00:00, and noon
is 12:00 PM equal to 12:00. If you are storing times in software, prefer the
24-hour form because it sorts correctly as text and avoids the AM/PM ambiguity
entirely.