← Back to challenges

12 vs 24 Hours

PythonHardstringsdatesformatting

Instructions

Create a function that converts 12-hour time to 24-hour time or vice versa. Return the output as a string.

Examples

convert_time("12:00 am") ➞ "0:00"

convert_time("6:20 pm") ➞ "18:20"

convert_time("21:00") ➞ "9:00 pm"

convert_time("5:05") ➞ "5:05 am"

Notes

  • A 12-hour time input will be denoted with an am or pm suffix.
  • A 24-hour input time contains no suffix.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.