Skip to content

Commit 18ec108

Browse files
committed
added edge cases
1 parent 29ef74a commit 18ec108

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
function formatAs12HourClock(time) {
2-
32
const hours = Number(time.slice(0, 2));
3+
const slicedMinutes = time.slice(-2);
44

5-
if (hours > 12) {
6-
return `${hours - 12}:00 pm`;
5+
if (hours === 0) {
6+
return `12:${slicedMinutes} am`;
7+
} else if (hours > 1 && hours <= 11) {
8+
return `0${hours}:${slicedMinutes} am`;
9+
} else if (hours === 12) {
10+
return `12:${slicedMinutes} pm`;
11+
} else if (hours > 12 && hours <= 21) {
12+
return `0${hours - 12}:${slicedMinutes} pm`;
13+
} else if (hours === 22 || hours === 23) {
14+
return `${hours - 12}:${slicedMinutes} pm`;
715
}
8-
return `${time} am`;
916
}
1017

11-
export {formatAs12HourClock};
18+
export { formatAs12HourClock };

0 commit comments

Comments
 (0)