-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopPanel.js
More file actions
34 lines (28 loc) · 1.19 KB
/
TopPanel.js
File metadata and controls
34 lines (28 loc) · 1.19 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
// Fetch current time and date
function getCurrentTimeAndDate() {
const currentDate = new Date();
const currentTime = currentDate.toLocaleTimeString();
const currentDay = currentDate.toLocaleDateString();
document.getElementById("current-time").textContent = currentTime;
document.getElementById("current-date").textContent = currentDay;
}
// Fetch current weather
function getCurrentWeather() {
const apiKey = "3e5375e4e6b0bcf722cee713718c51b9";
const city = "Cyberjaya";
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
const weatherDescription = data.weather[0].description;
const temperature = Math.round(data.main.temp - 273.15); // Convert Kelvin to Celsius
document.getElementById("current-weather").textContent = `${weatherDescription}, ${temperature}°C`;
})
.catch(error => {
console.error("Error fetching weather data:", error);
});
}
// Update time and date every second
setInterval(getCurrentTimeAndDate, 1000);
// Fetch current weather once
getCurrentWeather();