Skip to content

Commit 6c8b749

Browse files
committed
Fix leading zeros in 12-hour clock output
1 parent 650452e commit 6c8b749

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
function formatAs12HourClock(time) {
21

2+
function formatAs12HourClock(time) {
33
const hours = Number(time.slice(0, 2));
4-
const minutes = time.slice(3);
4+
const minutes = time.slice(3, 5);
55

66
if (hours === 0) {
77
return `12:${minutes} am`;
88
}
99

10-
if (hours === 12) {
11-
return `${time} pm`;
10+
if (hours < 12) {
11+
return `${String(hours).padStart(2, "0")}:${minutes} am`;
1212
}
1313

14-
if (hours > 12) {
15-
return `${hours - 12}:${minutes} pm`;
14+
if (hours === 12) {
15+
return `12:${minutes} pm`;
1616
}
1717

18-
return `${time} am`;
18+
return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`;
1919
}
2020

21-
export {formatAs12HourClock};
21+
export { formatAs12HourClock };

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
import {formatAs12HourClock} from "./timeConverter.js";
1+
2+
import { formatAs12HourClock } from "./timeConverter.js";
23
import assert from "node:assert";
34
import test from "node:test";
45

5-
test("correctly convert time after 12:00", function(){
6-
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
6+
test("correctly convert time after 12:00", function () {
7+
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
8+
});
9+
10+
test("can correctly convert morning time", function () {
11+
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
12+
});
13+
14+
test("correctly convert 13:00 with a leading zero", function () {
15+
assert.equal(formatAs12HourClock("13:00"), "01:00 pm");
16+
});
17+
18+
test("correctly keep the minutes", function () {
19+
assert.equal(formatAs12HourClock("13:45"), "01:45 pm");
20+
});
21+
22+
test("correctly convert midnight", function () {
23+
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
724
});
825

9-
test("can correctly convert morning time", function() {
10-
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
26+
test("correctly convert noon", function () {
27+
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
1128
});
1229

1330
test("correctly convert midnight", function() {
@@ -17,10 +34,10 @@ test("correctly convert noon", function() {
1734
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
1835
});
1936
test("correctly convert 13:00", function() {
20-
assert.equal(formatAs12HourClock("13:00"), "1:00 pm");
37+
assert.equal(formatAs12HourClock("13:00"), "01:00 pm");
2138
});
2239
test("correctly keep the minutes", function() {
23-
assert.equal(formatAs12HourClock("13:45"), "1:45 pm");
40+
assert.equal(formatAs12HourClock("13:45"), "01:45 pm");
2441
});
2542
test("correctly convert 12:30", function() {
2643
assert.equal(formatAs12HourClock("12:30"), "12:30 pm");

0 commit comments

Comments
 (0)