Skip to content

Commit 01c7b46

Browse files
author
AlanGit-debug2604
committed
Fix time formatting for hours >= 24 and ensure leading zeros for single-digit hours
1 parent 773c6bc commit 01c7b46

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

‎Sprint-3/5-stretch-extend/format-time.js‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
77
const minutes = time.slice(-2);
88
if (hours >= 24) {
9-
return `${hours - 24}:${minutes} am +1`;
9+
return `0${hours - 24}:${minutes} am +1`;
1010
} else if (hours > 12) {
1111
return `${hours - 12}:${minutes} pm`;
12+
} else if (hours > 9) {
13+
return `${hours}:${minutes} am`;
1214
}
13-
return `${hours}:${minutes} am`;
15+
return `0${hours}:${minutes} am`;
1416
}
1517

1618
const currentOutput = formatAs12HourClock("08:01");
17-
const targetOutput = "8:01 am";
19+
const targetOutput = "08:01 am";
1820
console.assert(
1921
currentOutput === targetOutput,
2022
`current output: ${currentOutput}, target output: ${targetOutput}`,
@@ -28,15 +30,31 @@ console.assert(
2830
);
2931

3032
const currentOutput3 = formatAs12HourClock("25:00");
31-
const targetOutput3 = "1:00 am +1";
33+
const targetOutput3 = "01:00 am +1";
3234
console.assert(
3335
currentOutput3 === targetOutput3,
3436
`current output: ${currentOutput3}, target output: ${targetOutput3}`,
3537
);
3638

39+
const currentOutput4 = formatAs12HourClock("00:01");
40+
const targetOutput4 = "00:01 am";
41+
console.assert(
42+
currentOutput4 === targetOutput4,
43+
`current output: ${currentOutput4}, target output: ${targetOutput4}`,
44+
);
45+
46+
const currentOutput5 = formatAs12HourClock("11:01");
47+
const targetOutput5 = "11:01 am";
48+
console.assert(
49+
currentOutput5 === targetOutput5,
50+
`current output: ${currentOutput5}, target output: ${targetOutput5}`,
51+
);
52+
3753
console.log(formatAs12HourClock("08:01"));
3854
console.log(formatAs12HourClock("23:55"));
3955
console.log(formatAs12HourClock("25:01"));
56+
console.log(formatAs12HourClock("00:01"));
57+
console.log(formatAs12HourClock("11:01"));
4058

4159
//Two patches
4260
//First hour input > 24, current algo is hours - 12 , e.g. 25-12 and print 13:00pm , rather than 1:00am (+1)

0 commit comments

Comments
 (0)