-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclock.html
70 lines (62 loc) · 2.14 KB
/
clock.html
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
<!DOCTYPE html>
<html>
<head>
<title>Date and Time Display</title>
<style>
body {
font-family: arial;
text-align: center;
}
.time {
font-size: 100px;
}
.date {
font-size: 75px;
}
#toggleButton {
border-radius: 15px;
padding: 15px;
font-size: 25px;
}
</style>
</head>
<body>
<div class="time" id="timeDisplay"></div>
<div class="date" id="dateDisplay"></div>
<br>
<button id="toggleButton">Toggle Time Format</button>
<script>
function getTimeString() {
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
// Convert to 12-hour format
var ampm = hours >= 12 ? 'PM' : 'AM';
var twelveHourFormat = ((hours + 11) % 12 + 1) + ":" + minutes + ":" + seconds + " " + ampm;
// Return time based on the current time format setting
return is24HourFormat ? currentTime.toTimeString().slice(0, 8) : twelveHourFormat;
}
function getDateString() {
var currentDate = new Date();
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
return currentDate.toLocaleDateString(undefined, options);
}
var is24HourFormat = true;
var timeDisplay = document.getElementById('timeDisplay');
var dateDisplay = document.getElementById('dateDisplay');
var toggleButton = document.getElementById('toggleButton');
function updateTimeAndDate() {
timeDisplay.textContent = getTimeString();
dateDisplay.textContent = getDateString();
}
updateTimeAndDate();
toggleButton.addEventListener('click', function () {
is24HourFormat = !is24HourFormat;
updateTimeAndDate();
});
// Update the time and date every second
setInterval(updateTimeAndDate, 1000);
</script>
</body>
</html>