Skip to content

Commit 8255bd2

Browse files
author
AlanGit-debug2604
committed
timeConverter.js to simplify hour formatting logic for hours > 12
1 parent 9bb46ff commit 8255bd2

1 file changed

Lines changed: 7 additions & 16 deletions

File tree

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
function formatAs12HourClock(time) {
2-
32
const hours = Number(time.slice(0, 2));
43
const minutes = time.slice(-2);
54

6-
if (hours > 22) {
7-
return `${hours - 12}:${minutes} pm`;
8-
}
9-
else if (hours > 12) {
5+
if (hours > 12) {
106
return `${hours - 12}:${minutes} pm`;
11-
}
12-
else if (hours === 12) {
7+
} else if (hours === 12) {
138
return `12:${minutes} pm`;
9+
} else if (hours > 9) {
10+
return `${hours}:${minutes} am`;
11+
} else if (hours < 1) {
12+
return `12:${minutes} am`;
1413
}
15-
16-
else if (hours > 9){
17-
return `${hours}:${minutes} am`
18-
}
19-
else if (hours < 1) {
20-
return `12:${minutes} am`;
21-
}
2214
return `${hours}:${minutes} am`;
2315
}
2416

25-
26-
export {formatAs12HourClock};
17+
export { formatAs12HourClock };

0 commit comments

Comments
 (0)