Amazingly, this simple task doesn’t seem to exist already, so…

Your task is to write a program that takes as input a 12 hour time, and converts it into “military time”, or 24-hour time format.

Input will be in the form:

HH:MM am/pm

Although slight variations are allowed:

  • The space separating the am/pm from the rest of the time is optional.

  • The last part can either be “am”/”pm”, or “a”/”p”.

  • Any capitalization is fine.

Output will be the inputted time, transformed into 24-hour format. It can be a number, or a string.

For both the input and output:

  • 0s in the first place are optional. 0s in the last 3 places are mandatory.

  • the delimiter separating hours and minutes can be a “:”, ” ” (a space), or nothing.

Other notes:

  • Midnight can be expressed either as 0000, or 2400.

  • Midnight will be considered “am”, while noon will be considered “pm”.

Your program can be a function, or a full program, and should either return the result, or output it to the stdout. Trailing whitespace is ok.

Examples (you aren’t required to support every format):

12:00am -> 0000
1200 pm -> 1200
1234Am  -> 00:34
134a    -> 134
01:34PM -> 13 34
1234pm  -> 1234  

This is code golf, so the smallest number of bytes wins. Since this is so trivial to solve using a built-in, it would be nice to see some code that manually solves this (but use of built-ins are fine).

Just to clarify, you aren’t required to support every possible format. Supporting only a single input and a single output format (of your choice) is fine. I would however like to limit the format as outlined above (which is already quite free).

, for example, would be unacceptable.