Skip to content

Commit 62033c7

Browse files
timeConverter.js fix edge cases for hours ,and put minutes for input in 12-hour clock conversion
1 parent 29ef74a commit 62033c7

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
function formatAs12HourClock(time) {
22

33
const hours = Number(time.slice(0, 2));
4+
const minutes = time.slice(-2);
45

5-
if (hours > 12) {
6-
return `${hours - 12}:00 pm`;
6+
if (hours > 22) {
7+
return `${hours - 12}:${minutes} pm`;
78
}
8-
return `${time} am`;
9+
else if (hours > 12) {
10+
return `${hours - 12}:${minutes} pm`;
11+
}
12+
else if (hours > 9){
13+
return `${hours}:${minutes} am`
14+
}
15+
else if (hours < 1) {
16+
return `12:${minutes} am`;
17+
}
18+
return `${hours}:${minutes} am`;
919
}
1020

21+
1122
export {formatAs12HourClock};

0 commit comments

Comments
 (0)