-
-
Notifications
You must be signed in to change notification settings - Fork 414
London | 26-ITP-Sep | Hanna Bohlin | Sprint 1 | formatAs12HourClock #1628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
58741a8
53b309b
6e1e485
c0c27ea
99591de
891851b
8c42ec0
72e8b26
ebfc759
7a51317
2c29789
e231337
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,25 @@ | ||
| function formatAs12HourClock(time) { | ||
|
|
||
| const hours = Number(time.slice(0, 2)); | ||
| const minutes = time.slice(-2); | ||
|
|
||
| let period = ""; | ||
| let hourString = ""; | ||
|
|
||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| if (hours <= 11){ | ||
| period = "am"; | ||
| } else { | ||
| period = "pm"; | ||
| } | ||
| return `${time} am`; | ||
|
|
||
| if (hours >= 13){ | ||
| hourString = hours - 12 < 10 ? `0${hours - 12}` : `${hours -12}`; | ||
| } else if (hours === 0){ | ||
| hourString = `${hours + 12}`; | ||
| } else { | ||
| hourString = hours < 10 ? `0${hours}`:`${hours}`; | ||
| } | ||
|
|
||
| return `${hourString}:${minutes} ${period}` | ||
| } | ||
|
|
||
| export {formatAs12HourClock}; | ||
| export { formatAs12HourClock }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,24 @@ | ||
| import {formatAs12HourClock} from "./timeConverter.js"; | ||
| import { formatAs12HourClock } from "./timeConverter.js"; | ||
| import assert from "node:assert"; | ||
| import test from "node:test"; | ||
|
|
||
| test("correctly convert time after 12:00", function(){ | ||
| assert.equal(formatAs12HourClock("23:00"), "11:00 pm"); | ||
| }); | ||
| test("can format time after 12:00", () => | ||
| assert.equal(formatAs12HourClock("23:00"), "11:00 pm")); | ||
|
|
||
| test("can correctly convert morning time", function() { | ||
| assert.equal(formatAs12HourClock("08:00"), "08:00 am"); | ||
| }); | ||
| test("can format morning time", () => | ||
| assert.equal(formatAs12HourClock("08:00"), "08:00 am")); | ||
|
|
||
| test("can format noon time", () => | ||
| assert.equal(formatAs12HourClock("12:00"), "12:00 pm")); | ||
|
|
||
| test("can format afternoon time with minutes other than 00", () => | ||
| assert.equal(formatAs12HourClock("15:45"), "03:45 pm")); | ||
|
|
||
| test("can format morning time with minutes other than 00", () => | ||
| assert.equal(formatAs12HourClock("08:25"), "08:25 am")); | ||
|
|
||
| test("can format early noon with minutes other than 00", () => | ||
| assert.equal(formatAs12HourClock("12:17"), "12:17 pm")); | ||
|
|
||
| test("can format time between midnight and 1 am", () => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| assert.equal(formatAs12HourClock("00:15"), "12:15 am")); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: when
hoursis 0, what ishours + 12always going to be? Would writing that value directly make this branch easier to read?