forked from Technigo/project-weather-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (55 loc) · 2.16 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// DOM / API
const city = document.getElementById("city");
const weatherToday = document.getElementById("weatherToday");
const temperature = document.getElementById("temperature");
const sunrise = document.getElementById("sunrise");
const sunset = document.getElementById("sunset");
const weatherIcon = document.querySelector('.icon');
const weatherApi = "https://api.openweathermap.org/data/2.5/weather?q=Stockholm,Sweden&units=metric&APPID=515087c7fb02c4b2d4dca12b9e40bb14";
//TODAY'S WEATHER - CITY, TEMP
const getCurrentWeatherData = () => {
fetch('https://api.openweathermap.org/data/2.5/weather?q=Stockholm,Sweden&units=metric&APPID=515087c7fb02c4b2d4dca12b9e40bb14')
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
temperature.textContent=data.main.temp
city.textContent=data.name
weatherToday.textContent=data.weather[0].description
weatherIcon.src = `http://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png`
// SUNRISE AND SUNSET SHOWING
sunrise.textContent=`Sunrise: ${new Date(data.sys.sunrise * 1000).toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
})}`
sunset.textContent=`Sunset: ${new Date(data.sys.sunset * 1000).toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
})}`
})
}
getCurrentWeatherData();
// 5 DAY WEATHER FORECAST WITH TEMPERETURE
const forecastFiveDayAndTemp = () => {
const forecastFiveDayAndTemp = document.getElementById("forecastFiveDayAndTemp");
fetch('https://api.openweathermap.org/data/2.5/forecast?q=Stockholm,Sweden&units=metric&APPID=515087c7fb02c4b2d4dca12b9e40bb14')
.then((response) => {
return response.json();
})
.then((json) => {
const filteredForecast = json.list.filter(item =>
item.dt_txt.includes('12:00'));
console.log(filteredForecast);
filteredForecast.forEach(item => {
const date = new Date(item.dt * 1000);
const dayName = date.toLocaleDateString("en-US", {
weekday: "long"
});
const dayTemperature = item.main.temp.toFixed(1);
console.log(date, dayName, dayTemperature);
forecastFiveDayAndTemp.innerHTML+= `<h5>${dayName}: ${dayTemperature} °C</h5>`
})
})
}
forecastFiveDayAndTemp();