-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
124 lines (101 loc) · 4.28 KB
/
script.js
File metadata and controls
124 lines (101 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Function to convert time
function convertTime() {
let inputTime = document.getElementById("timeInput").value.trim();
// Normalize input by replacing '.' with ':'
inputTime = inputTime.replace('.', ':');
// Check if input matches HH:MM format
if (/^(?:[01]?\d|2[0-3]):[0-5]\d$/.test(inputTime)) {
const [hours, minutes] = inputTime.split(":").map(Number);
let period = "AM";
let convertedHours = hours;
// Convert to 12-hour format
if (hours === 0) {
convertedHours = 12; // Midnight
} else if (hours === 12) {
period = "PM"; // Noon
} else if (hours > 12) {
convertedHours = hours - 12;
period = "PM";
}
// Format hours and minutes to always have two digits
const formattedHours = String(convertedHours).padStart(2, "0");
const formattedMinutes = String(minutes).padStart(2, "0");
const formattedTime = `${formattedHours}:${formattedMinutes} ${period}`;
document.getElementById("result").innerText = `Converted Time: ${formattedTime}`;
} else {
// Alert the user for invalid input
alert("Please enter a valid time in HH:MM format (24-hour format).");
document.getElementById("result").innerText = "";
}
}
// Function to clear input and result
function clearFields() {
document.getElementById("timeInput").value = ""; // Clear the input field
document.getElementById("result").innerText = ""; // Clear the result display
}
function updateClock() {
const now = new Date();
let hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
// Determine AM or PM suffix
const ampm = hours >= 12 ? 'PM' : 'AM';
// Convert to 12-hour format
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
// Format minutes and seconds to always be two digits
const formattedMinutes = String(minutes).padStart(2, '0');
const formattedSeconds = String(seconds).padStart(2, '0');
// Construct the time string
const timeString = `${String(hours).padStart(2, '0')}:${formattedMinutes}:${formattedSeconds} ${ampm}`;
// Update the clock display
document.getElementById('clock').textContent = timeString;
}
// Update the clock every second
setInterval(updateClock, 1000);
updateClock(); // Initial call to set the clock immediately
function updateClock2() {
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
// Format hours, minutes, and seconds to always be two digits
const formattedHours = String(hours).padStart(2, '0');
const formattedMinutes = String(minutes).padStart(2, '0');
const formattedSeconds = String(seconds).padStart(2, '0');
// Construct the time string in 24-hour format
const timeString = `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
// Update the clock display
document.getElementById('clock2').textContent = timeString;
}
// Update the clock every second
setInterval(updateClock2, 1000);
updateClock2(); // Initial call to set the clock immediately
// Event listener for convert button click
document.getElementById("convertButton").addEventListener("click", convertTime);
// Event listener for clear button click
document.getElementById("clearButton").addEventListener("click", clearFields);
// Event listener for Enter key press
document.getElementById("timeInput").addEventListener("keydown", function (event) {
if (event.key === "Enter") {
convertTime();
}
});
// Event listener for input field to clear result when empty
document.getElementById("timeInput").addEventListener("input", function () {
if (this.value.trim() === "") {
document.getElementById("result").innerText = ""; // Clear the result
}
});
// Service Worker registration
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('./service-worker.js')
.then(registration => {
console.log('Service Worker registered with scope:', registration.scope);
})
.catch(error => {
console.error('Service Worker registration failed:', error);
});
});
}