File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11function formatAs12HourClock ( time ) {
2+ let hours = Number ( time . slice ( 0 , 2 ) ) ;
3+ const minutes = time . slice ( 3 ) ;
24
3- const hours = Number ( time . slice ( 0 , 2 ) ) ;
5+ const period = hours >= 12 ? "pm" : "am" ;
46
5- if ( hours > 12 ) {
6- return `${ hours - 12 } :00 pm` ;
7- }
8- return `${ time } am` ;
7+ if ( hours === 0 ) {
8+ hours = 12 ;
9+ } else if ( hours > 12 ) {
10+ hours -= 12 ;
11+ }
12+
13+ const formattedHours =
14+ hours < 10 && hours !== 12 ? `0${ hours } ` : hours ;
15+
16+ return `${ formattedHours } :${ minutes } ${ period } ` ;
917}
1018
11- export { formatAs12HourClock } ;
19+ export { formatAs12HourClock } ;
Original file line number Diff line number Diff line change 1- import { formatAs12HourClock } from "./timeConverter.js" ;
1+ import { formatAs12HourClock } from "./timeConverter.js" ;
22import assert from "node:assert" ;
33import test from "node:test" ;
44
5- test ( "correctly convert time after 12:00" , function ( ) {
5+ test ( "correctly convert time after 12:00" , function ( ) {
66 assert . equal ( formatAs12HourClock ( "23:00" ) , "11:00 pm" ) ;
77} ) ;
88
9- test ( "can correctly convert morning time" , function ( ) {
9+ test ( "can correctly convert morning time" , function ( ) {
1010 assert . equal ( formatAs12HourClock ( "08:00" ) , "08:00 am" ) ;
1111} ) ;
12+
13+ test ( "converts midnight (00:00)" , function ( ) {
14+ assert . equal ( formatAs12HourClock ( "00:00" ) , "12:00 am" ) ;
15+ } ) ;
16+
17+ test ( "converts noon (12:00)" , function ( ) {
18+ assert . equal ( formatAs12HourClock ( "12:00" ) , "12:00 pm" ) ;
19+ } ) ;
You can’t perform that action at this time.
0 commit comments