Skip to content

Commit 710770b

Browse files
cyfcyf
authored andcommitted
Fix 12-hour clock conversion edge cases
1 parent 92fc81c commit 710770b

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎format-clock-edge-cases/timeConverter.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
function formatAs12HourClock(time) {
22
const hours = Number(time.slice(0, 2));
33
if (hours === 0) {
4-
return `${hours + 12}${time.slice(2, 5)} am`;
4+
return `${String(hours + 12).padStart(2, "0")}${time.slice(2, 5)} am`;
5+
}
6+
if (hours === 12) {
7+
return `${time} pm`;
58
}
69
if (hours > 12) {
7-
return `${hours - 12}${time.slice(2, 5)} pm`;
10+
return `${String(hours - 12).padStart(2, "0")}${time.slice(2, 5)} pm`;
811
}
912
return `${time} am`;
1013
}

‎format-clock-edge-cases/timeConverter.test.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ test("can correctly convert morning time", function () {
1313
test("can correctly convert midnight time", function () {
1414
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
1515
});
16+
17+
test("can correctly convert minutes", function () {
18+
assert.equal(formatAs12HourClock("21:08"), "09:08 pm");
19+
});
20+
21+
test("can correctly convert noon", function () {
22+
assert.equal(formatAs12HourClock("12:30"), "12:30 pm");
23+
});

0 commit comments

Comments
 (0)