Skip to content

Commit b3e50e6

Browse files
refactor timeConverter.js to improve hour formatting logic
1 parent 2d554c7 commit b3e50e6

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
function formatAs12HourClock(time) {
2+
const [hoursStr, minutesStr] = time.split(":");
3+
let hours = Number(hoursStr);
24

3-
const hours = Number(time.slice(0, 2));
5+
const period = hours >= 12 ? "pm" : "am";
46

5-
if (hours > 12) {
6-
return `${hours - 12}:00 pm`;
7+
if (hours === 0) {
8+
hours = 12;
9+
} else if (hours > 12) {
10+
hours = hours - 12;
711
}
8-
return `${time} am`;
9-
}
1012

11-
export {formatAs12HourClock};
13+
const formattedHours = hours.toString().padStart(2, "0");
1214

15+
return `${formattedHours}:${minutesStr} ${period}`;
16+
}
1317

18+
export { formatAs12HourClock };

0 commit comments

Comments
 (0)