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").
- Convert the hours to a 12-hour format.
- Suffix it with "AM" or "PM".
- Return the new string (e.g., "2:30 PM").
The Rules
00:00to00:59becomes12:MM AM01:00to11:59becomesHH:MM AM12:00to12:59becomes12:MM PM13:00to23:59becomes[HH-12]:MM PM
Tip: You can use
timeStr.split(':')to get an array of [hours, minutes]. Convert the hours string to a number usingNumber()first!
01EXAMPLE 1
Input
timeStr = "14:30"Output
"2:30 PM"Explanation: 14:00 is 2:00 PM.
02EXAMPLE 2
Input
timeStr = "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
JavaScriptSystem handles I/O — write your function only
Loading...
5 Hidden
Input Arguments
timeStr"14:30"
Expected Output
2:30 PM
Click RUN to test your solution