-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
88 lines (69 loc) · 2.95 KB
/
code.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
let cityname = document.querySelector(".weather_city");
let datetime = document.querySelector(".weather_date_time");
let w_forecast = document.querySelector(".weather_forecast");
let w_icon = document.querySelector(".weather_icon");
let w_temperature = document.querySelector(".weather_temperature");
let w_minterm = document.querySelector(".weather_min");
let w_maxterm = document.querySelector(".weather_max");
let w_feelslike = document.querySelector(".weather_feelslike");
let w_humidity = document.querySelector(".weather_humidity");
let w_wind = document.querySelector(".weather_wind");
let w_pressure = document.querySelector(".weather_pressure");
let citySearch = document.querySelector(".weather_search");
// full country name using the of method
const getcountryName = (code) => {
return new Intl.DisplayNames([code], { type: "region" }).of(code);
};
// created the date and dtime
const getDateTime = (dt) => {
//let dt = 1729699653;
const currdate = new Date(dt * 1000);
console.log(currdate);
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric"
};
const formatter = new Intl.DateTimeFormat("en-US", options);
console.log(formatter);
return formatter.format(currdate);
};
let city = "pune";
// searching the city
citySearch.addEventListener('submit',(e)=>{
let cityname = document.querySelector(".city_name");
e.preventDefault();
console.log(cityname.value);
city = cityname.value;
// this is using for call function
getweatherData();
// this is using for cleaning the function
cityname.value = "";
});
const getweatherData = async () => {
//const weatherUrl = `https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid=daa5c17ee64f6aca6c86e7986941e76c`;
const weatherUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=a0b06669a793506ae54060f7df8a5fb6`;
try {
const res = await fetch(weatherUrl);
const data = await res.json();
console.log(data);
const { main, name, weather, wind, sys, dt } = data;
cityname.innerHTML = `${name} , ${getcountryName(sys.country)} `;
datetime.innerHTML = getDateTime(dt);
w_forecast.innerHTML = weather[0].main;
w_icon.innerHTML = `<img src="http://openweathermap.org/img/wn/${weather[0].icon}@2x.png" alt="Weather Icon">`;
w_temperature.innerHTML = `Max:${main.temp}°`;
w_minterm.innerHTML = `Min:${main.temp_min.toFixed()}°`;
// w_maxterm.innerHTML = `${main.temp_max.toFixed()}°`;
w_feelslike.innerHTML = `${main.feels_like.toFixed(2)} K`;
w_humidity.innerHTML = `${main.humidity} %`;
w_wind.innerHTML = `${wind.speed} m/s`;
w_pressure.innerHTML = `${main.pressure} hpa`;
} catch (error) {
console.log("error");
}
}
document.body.addEventListener("load", getweatherData());