|
1 | 1 | function formatAs12HourClock(time) { |
2 | | - if (time.length == 4) { |
3 | | - c; |
4 | | - time = time.padStart(5, "0"); |
5 | | - } |
| 2 | + if (typeof time !== "string") { |
| 3 | + return "Incorrect timing format"; |
| 4 | + } |
| 5 | + const correctTimingFormat = time.match(/^(\d{1,2}):(\d{2})$/); |
6 | 6 |
|
7 | | - if (typeof time !== "string") { |
8 | | - return "nccorect timing format"; |
| 7 | + if (!correctTimingFormat) { |
| 8 | + return "Incorrect timing format"; |
9 | 9 | } |
10 | 10 |
|
11 | | - const match = time.match(/^(\d{1,2}):(\d{2})$/); |
| 11 | + if (time.length == 4) { |
| 12 | + time = time.padStart(5, "0"); |
| 13 | + } |
12 | 14 |
|
13 | | - if (!match) { |
14 | | - return "Inccorect timing format"; |
15 | | - } |
| 15 | + const match = time.match(/^(\d{1,2}):(\d{2})$/); |
16 | 16 |
|
17 | | - const hours = Number(time.slice(0, 2)); |
18 | | - const minutes = Number(time.slice(3, 5)); |
| 17 | + if (!match) { |
| 18 | + return "Incorrect timing format"; |
| 19 | + } |
19 | 20 |
|
20 | | - if (time === "12:00") { |
21 | | - return `12:00 pm`; |
22 | | - } |
| 21 | + const hours = Number(time.slice(0, 2)); |
| 22 | + const minutes = Number(time.slice(3, 5)); |
23 | 23 |
|
24 | | - if (hours < 12) { |
25 | | - return `${time} am`; |
26 | | - } |
27 | | - if (hours >= 12) { |
28 | | - return `${time} pm`; |
29 | | - } |
30 | | - if (hours == 24) { |
31 | | - return `12:00 am`; |
32 | | - } |
| 24 | + // 00:00 |
| 25 | + if (time === "00:00") { |
| 26 | + return `12:00 am`; |
| 27 | + } |
33 | 28 |
|
34 | | - if (hours > 12 && hours < 22 && minutes < 10) { |
35 | | - return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`; |
36 | | - } |
| 29 | + // 12:00 |
| 30 | + if (time === "12:00") { |
| 31 | + return `12:00 pm`; |
| 32 | + } |
37 | 33 |
|
38 | | - if (hours > 12 && hours < 22 && minutes < 10) { |
39 | | - return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`; |
40 | | - } |
41 | | - if (hours > 12 && hours < 22 && minutes >= 10) { |
42 | | - return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`; |
43 | | - } |
44 | | - if (hours >= 22 && minutes < 10) { |
45 | | - return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`; |
46 | | - } |
47 | | - if (hours >= 22 && minutes >= 10) { |
48 | | - return `${hours - 12}:${minutes} pm`; |
49 | | - } |
50 | | - if (hours >= 12 && minutes >= 10) { |
51 | | - return `${hours}:${minutes} pm`; |
52 | | - } |
| 34 | + // 00:xx |
| 35 | + if (hours === 0) { |
| 36 | + return `12:${minutes.toString().padStart(2, "0")} am`; |
| 37 | + } |
| 38 | + |
| 39 | + // 01:xx - 11:xx |
| 40 | + if (hours < 12) { |
| 41 | + return `${time} am`; |
| 42 | + } |
| 43 | + |
| 44 | + // 12:xx |
| 45 | + if (hours === 12) { |
| 46 | + return `${time} pm`; |
| 47 | + } |
| 48 | + |
| 49 | + // 13:00 - 21:59 |
| 50 | + if (hours > 12 && hours < 22 && minutes < 10) { |
| 51 | + return `${(hours - 12).toString().padStart(2, "0")}:${minutes |
| 52 | + .toString() |
| 53 | + .padStart(2, "0")} pm`; |
| 54 | + } |
| 55 | + |
| 56 | + if (hours > 12 && hours < 22 && minutes >= 10) { |
| 57 | + return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`; |
| 58 | + } |
| 59 | + |
| 60 | + // 22:00 - 23:59 |
| 61 | + if (hours >= 22 && minutes < 10) { |
| 62 | + return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`; |
| 63 | + } |
| 64 | + |
| 65 | + if (hours >= 22 && minutes >= 10) { |
| 66 | + return `${hours - 12}:${minutes} pm`; |
| 67 | + } |
53 | 68 | } |
54 | 69 |
|
55 | 70 | export { formatAs12HourClock }; |
0 commit comments