Skip to content

Commit 84e5b3c

Browse files
committed
update: padding for hour
1 parent 0d71e05 commit 84e5b3c

File tree

1 file changed

+34
-2
lines changed
  • Sprint-1/2-mandatory-errors

1 file changed

+34
-2
lines changed

Sprint-1/2-mandatory-errors/4.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
1-
const 12HourClockTime = "20:53";
2-
const 24hourClockTime = "08:53";
1+
// const 12HourClockTime = "20:53"; it is in 24 hour format
2+
// const 24hourClockTime = "08:53"; it is in 12 hour format then we will write correctly
3+
// const twelveHourClockTime = "08:53 PM"; // Example 12-hour format time
4+
// const twentyFourHourClockTime = "20:53"; // Example 24-hour format time
5+
6+
const twelveHourClockTime = "08:53 PM"; // Example 12-hour format time
7+
const twentyFourHourClockTime = "20:53"; // Example 24-hour format time
8+
function convertTo12HourClockTime(time24clock) {
9+
let [hours, minutes] = time24clock.split(":").map(Number);
10+
const modifier = hours >= 12 ? "PM" : "AM";
11+
12+
if (hours === 0) {
13+
hours = 12; // Midnight case
14+
} else if (hours > 12) {
15+
hours -= 12; // Convert to 12-hour format
16+
}
17+
18+
return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} ${modifier}`;
19+
}
20+
function convertTo24HourClockTime(time12clock) {
21+
let [time, modifier] = time12clock.split(" ");
22+
let [hours, minutes] = time.split(":").map(Number);
23+
24+
if (modifier === "PM" && hours !== 12) {
25+
hours += 12;
26+
} else if (modifier === "AM" && hours === 12) {
27+
hours = 0;
28+
}
29+
30+
return `${hours.toString().padStart(2, "0")}:${minutes
31+
.toString()
32+
.padStart(2, "0")}`;
33+
}
34+
console.log(convertTo12HourClockTime(twentyFourHourClockTime)); // Output: "08:53 PM"

0 commit comments

Comments
 (0)