Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions format-clock-edge-cases/timeConverter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function formatAs12HourClock(time) {

const hours = Number(time.slice(0, 2));
const minutes = time.slice(3);

const period = hours >= 12 ? "pm" : "am";
const hours12 = hours % 12 === 0 ? 12 : hours % 12;

if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
return `${String(hours12).padStart(2, "0")}:${minutes} ${period}`;
Comment thread
lwandogwele52-ui marked this conversation as resolved.
Resolved
}

export {formatAs12HourClock};
29 changes: 29 additions & 0 deletions format-clock-edge-cases/timeConverter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,32 @@ test("correctly convert time after 12:00", function(){
test("can correctly convert morning time", function() {
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
});

test("midnight hour becomes 12 am", function() {
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
assert.equal(formatAs12HourClock("00:30"), "12:30 am");
assert.equal(formatAs12HourClock("00:59"), "12:59 am");
});

test("early morning hours are padded and stay am", function() {
assert.equal(formatAs12HourClock("01:00"), "01:00 am");
assert.equal(formatAs12HourClock("09:05"), "09:05 am");
assert.equal(formatAs12HourClock("11:59"), "11:59 am");
});

test("noon hour becomes 12 pm", function() {
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
assert.equal(formatAs12HourClock("12:30"), "12:30 pm");
assert.equal(formatAs12HourClock("12:59"), "12:59 pm");
});

test("afternoon hours are padded and keep their minutes", function() {
assert.equal(formatAs12HourClock("13:00"), "01:00 pm");
assert.equal(formatAs12HourClock("15:45"), "03:45 pm");
assert.equal(formatAs12HourClock("21:07"), "09:07 pm");
});

test("late evening hours stay pm", function() {
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
});
Loading