-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (54 loc) · 2.38 KB
/
Copy pathscript.js
File metadata and controls
64 lines (54 loc) · 2.38 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
// Define time zones with their UTC offsets and city names
const timeZones = {
'newyork': { name: 'New York', offset: -5 }, // EST
'losangeles': { name: 'Los Angeles', offset: -8 }, // PST
'toronto': { name: 'Toronto', offset: -5 }, // EST
'london': { name: 'London', offset: 0 }, // GMT
'paris': { name: 'Paris', offset: 1 }, // CET
'dubai': { name: 'Dubai', offset: 4 }, // GST
'mumbai': { name: 'Mumbai', offset: 5.5 }, // IST
'singapore': { name: 'Singapore', offset: 8 }, // SGT
'hongkong': { name: 'Hong Kong', offset: 8 }, // HKT
'tokyo': { name: 'Tokyo', offset: 9 }, // JST
'sydney': { name: 'Sydney', offset: 11 }, // AEDT
'saopaulo': { name: 'São Paulo', offset: -3 } // BRT
};
// Function to format time with leading zeros
function formatTime(hours, minutes, seconds) {
return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
}
// Function to format date
function formatDate(date) {
const options = { weekday: 'short', month: 'short', day: 'numeric' };
return date.toLocaleDateString('en-US', options);
}
// Function to get time for a specific timezone
function getTimeForZone(offset) {
const now = new Date();
const utc = now.getTime() + now.getTimezoneOffset() * 60000;
const zoneDate = new Date(utc + 3600000 * offset);
return zoneDate;
}
// Function to update a single clock
function updateClock(zoneKey, zoneData) {
const zoneDate = getTimeForZone(zoneData.offset);
const hours = zoneDate.getHours();
const minutes = zoneDate.getMinutes();
const seconds = zoneDate.getSeconds();
const formattedTime = formatTime(hours, minutes, seconds);
const formattedDate = formatDate(zoneDate);
const timeElement = document.getElementById(`time-${zoneKey}`);
const dateElement = document.getElementById(`date-${zoneKey}`);
if (timeElement) timeElement.textContent = formattedTime;
if (dateElement) dateElement.textContent = formattedDate;
}
// Function to update all clocks
function updateAllClocks() {
Object.keys(timeZones).forEach(zoneKey => {
updateClock(zoneKey, timeZones[zoneKey]);
});
}
// Update clocks immediately on page load
updateAllClocks();
// Update clocks every 1000ms (1 second)
setInterval(updateAllClocks, 1000);