-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (58 loc) · 2.12 KB
/
Copy pathscript.js
File metadata and controls
75 lines (58 loc) · 2.12 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
// Set Date
function displayDate() {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const dateElements = document.querySelectorAll('.date');
dateElements.forEach(el => el.textContent = `${year}-${month}-${day}`);
}
// Set time
var oldTime;
function setTime() {
const now = new Date();
const time = now.toLocaleTimeString('en-US', {
hour12: false,
hour: 'numeric',
minute: 'numeric'
});
if (oldTime === time) return;
oldTime = time;
const [hours, minutes] = time.split(':');
document.querySelector('#hours').innerText = hours.padStart(2, '0');
document.querySelector('#minutes').innerText = minutes.padStart(2, '0');
}
// Get weather
async function fetchWeather() {
const iconEl = document.getElementById('weather-icon');
const tempEl = document.getElementById('weather-temp');
const descEl = document.getElementById('weather-desc');
try {
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${CITY}&units=metric&appid=${API_KEY}`);
if (!response.ok) throw new Error('API error');
const data = await response.json();
const description = capitalize(data.weather[0].description);
const temp = Math.round(data.main.temp);
const iconCode = data.weather[0].icon;
iconEl.src = `https://openweathermap.org/img/wn/${iconCode}@4x.png`;
iconEl.alt = description;
tempEl.textContent = `${temp}°C`;
descEl.textContent = `${description}`;
} catch (error) {
console.error('Weather fetch error:', error);
tempEl.textContent = '--';
descEl.textContent = 'Weather unavailable';
iconEl.style.display = 'none';
}
}
function capitalize(text) {
return text.charAt(0).toUpperCase() + text.slice(1);
}
// Load page
document.addEventListener('DOMContentLoaded', function() {
displayDate();
setTime();
fetchWeather();
setInterval(setTime, 1000);
setInterval(fetchWeather, 900000)
});