-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
49 lines (39 loc) · 1.47 KB
/
script.js
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
var is24HourFormat = false;
var showSeconds = true; // Added variable to track if seconds should be shown or hidden
// Function to update the clock time and date
function updateClock() {
var now = new Date();
var options = {
hour12: !is24HourFormat,
hour: 'numeric',
minute: 'numeric',
second: showSeconds ? 'numeric' : undefined // Show seconds if showSeconds is true
};
var time = now.toLocaleTimeString('en-US', options);
var date = getFormattedDate(now);
document.getElementById('time').textContent = time;
document.getElementById('date').textContent = date;
}
// Function to get the formatted date string
function getFormattedDate(date) {
var weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var dayOfWeek = weekdays[date.getDay()];
var day = date.getDate();
var month = months[date.getMonth()];
return dayOfWeek + ', ' + day + ' ' + month;
}
// Function to toggle between 12-hour and 24-hour formats
function toggleTimeFormat() {
is24HourFormat = !is24HourFormat;
updateClock();
}
// Function to show or hide seconds
function toggleSeconds() {
showSeconds = !showSeconds;
updateClock();
}
// Update the clock every second
setInterval(updateClock, 1000);
// Initial call to display the clock
updateClock();