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- console . log ( time . length ) ;
3-
42 if ( time . length == 4 ) {
5- console . log ( "got here" ) ;
3+ c ;
64 time = time . padStart ( 5 , "0" ) ;
75 }
86
@@ -19,21 +17,24 @@ function formatAs12HourClock(time) {
1917 const hours = Number ( time . slice ( 0 , 2 ) ) ;
2018 const minutes = Number ( time . slice ( 3 , 5 ) ) ;
2119
22- if ( time === "00:00" ) {
23- return `12:00 am` ;
24- }
2520 if ( time === "12:00" ) {
2621 return `12:00 pm` ;
2722 }
2823
2924 if ( hours < 12 ) {
3025 return `${ time } am` ;
31- } else if ( hours == 12 ) {
32- return `${ time } am` ;
33- } else if ( hours == 24 ) {
26+ }
27+ if ( hours >= 12 ) {
28+ return `${ time } pm` ;
29+ }
30+ if ( hours == 24 ) {
3431 return `12:00 am` ;
3532 }
3633
34+ if ( hours > 12 && hours < 22 && minutes < 10 ) {
35+ return `${ ( hours - 12 ) . toString ( ) . padStart ( 2 , "0" ) } :${ minutes . toString ( ) . padStart ( 2 , "0" ) } pm` ;
36+ }
37+
3738 if ( hours > 12 && hours < 22 && minutes < 10 ) {
3839 return `${ ( hours - 12 ) . toString ( ) . padStart ( 2 , "0" ) } :${ minutes . toString ( ) . padStart ( 2 , "0" ) } pm` ;
3940 }
@@ -46,6 +47,9 @@ function formatAs12HourClock(time) {
4647 if ( hours >= 22 && minutes >= 10 ) {
4748 return `${ hours - 12 } :${ minutes } pm` ;
4849 }
50+ if ( hours >= 12 && minutes >= 10 ) {
51+ return `${ hours } :${ minutes } pm` ;
52+ }
4953}
5054
5155export { formatAs12HourClock } ;
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ test("can corectly convert mid-day", () =>
1717 assert . equal ( formatAs12HourClock ( "12:00" ) , "12:00 pm" ) ) ;
1818
1919test ( "can corectly convert half an hour passed midnight" , ( ) =>
20- assert . equal ( formatAs12HourClock ( "00:30" ) , "00 :30 am" ) ) ;
20+ assert . equal ( formatAs12HourClock ( "00:30" ) , "12 :30 am" ) ) ;
2121
2222test ( "can correctly convert time with missing leading zero" , function ( ) {
2323 assert . equal ( formatAs12HourClock ( "9:00" ) , "09:00 am" ) ;
You can’t perform that action at this time.
0 commit comments