Skip to content

Commit e6fde3b

Browse files
author
russom
committed
Add padStart method to convert 23:00 to 01:00pm
1 parent 598f744 commit e6fde3b

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

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

3-
const hours = Number(time.slice(0, 2).padStart(2, 0));
44
const minutes = time.slice(3, 5);
55

66
if (hours === 0) {
7-
return `12:${minutes} AM`
7+
return `12:${minutes} AM`;
88
}
99
if (hours === 12) {
1010
return `12:${minutes} PM`;
1111
}
1212
if (hours > 12) {
13-
return `${hours - 12}:${minutes} PM`;
13+
const paddedHours = String(hours - 12).padStart(2, "0");
14+
return `${paddedHours}:${minutes} PM`;
1415
}
1516
return `${time} AM`;
1617
}
1718

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

0 commit comments

Comments
 (0)