-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.js
More file actions
80 lines (68 loc) · 2.83 KB
/
Function.js
File metadata and controls
80 lines (68 loc) · 2.83 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
76
77
78
79
80
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_minTem = document.querySelector(".weather_min");
let w_maxTem = document.querySelector(".weather_max");
// part 3 reference
let weather_feel = document.querySelector(".weather_feelsLike");
let weather_humidity = document.querySelector(".weather_humidity");
let weather_wind = document.querySelector(".weather_wind");
let weather_pressure=document.querySelector(".weather_pressure");
// when you make a request using the fetch() function, the response contains data in the form of a JSON string. The res.json() method is used to asynchronously parse JSON string into a Javascript object.
let citySearch = document.querySelector(".weather_search");
//here getting country code
const getCountryName=(code)=>{
return new Intl.DisplayNames([code], { type: "region" }).of(code);
}
//To get date and time
const getDateTime=(dt)=>{
const curDate = new Date(dt * 1000);
console.log(curDate);
const options ={
weekday: "long",
year:"numeric",
month:"long",
day:"numeric",
hour:"numeric",
minute:"numeric",
// second:"numeric" ,
};
const formatter = new Intl.DateTimeFormat("en-US",options);
console.log(formatter);
return formatter.format(curDate);
}
let city ="pune";
// search functionality
citySearch.addEventListener("submit",(e)=>{
e.preventDefault();
let cityName=document.querySelector(".city_name");
city=cityName.value;
getWeatherData();
cityName.value="";
});
const getWeatherData = async ()=>{
const weatherUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&APPID=848e99beedb48e3e249e75eb3efffe06`;
try{
const res = await fetch (weatherUrl);
const data = await res.json();
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="https://openweathermap.org/img/wn/${weather[0].icon}@4x.png"/>`;
w_temperature.innerHTML=`${main.temp}°`;
w_minTem.innerHTML=`Min: ${main.temp_min.toFixed()}°`
w_maxTem.innerHTML=`Max: ${main.temp_max.toFixed()}°`
// 3rd part
weather_feel.innerHTML=`${main.feels_like.toFixed(2)}°`;
weather_humidity.innerHTML=`${main.humidity}%`;
weather_wind.innerHTML=`${wind.speed}m/s`;
weather_pressure.innerHTML=`${main.pressure}hPa`;
} catch (error){
console.log(error);
cityName.innerHTML=error;
}
}
document.body.addEventListener("load",getWeatherData());