← Back to challenges

12 vs 24 Hours

JavaScriptHardstringsdatesformatting

Instructions

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

Examples

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

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

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

convertTime("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.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.