The Chronos Converter

MediumAcc. 84.2%
+30 XP 12

Translating Time

In the world of technology, time is often tracked using a 24-hour clock (military time). To make it friendly for the average person, we must convert it to a 12-hour format with the correct "AM" or "PM" suffix.

The Assignment

Your function receives a string timeStr in the format "HH:MM" (e.g., "14:30").

  1. Convert the hours to a 12-hour format.
  2. Suffix it with "AM" or "PM".
  3. Return the new string (e.g., "2:30 PM").

The Rules

  • 00:00 to 00:59 becomes 12:MM AM
  • 01:00 to 11:59 becomes HH:MM AM
  • 12:00 to 12:59 becomes 12:MM PM
  • 13:00 to 23:59 becomes [HH-12]:MM PM

Tip: You can use timeStr.split(':') to get an array of [hours, minutes]. Convert the hours string to a number using Number() first!

01EXAMPLE 1

InputtimeStr = "14:30"
Output"2:30 PM"

Explanation: 14:00 is 2:00 PM.

02EXAMPLE 2

InputtimeStr = "00:15"
Output"12:15 AM"

Explanation: 00:00 is midnight.

Constraints

  • Input is always a valid "HH:MM" string.
  • Minutes should remain exactly as they are in the input.
  • Do not add leading zeros to the resulting hours (e.g. "2:30 PM", not "02:30 PM").
Control FlowStringsLogic
JavaScript
Loading...
5 Hidden

Input Arguments

timeStr"14:30"

Expected Output

2:30 PM

Click RUN to test your solution