-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.js
More file actions
85 lines (64 loc) · 2.99 KB
/
Script.js
File metadata and controls
85 lines (64 loc) · 2.99 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
81
82
83
84
85
const search = document.querySelector('.search');
const country_inp = document.querySelector('.country_inp');
const date_value = document.querySelector('.date');
const weather_feels = document.querySelector('.weather_feels');
const weather_png = document.querySelector('.weather_png');
const temp = document.querySelector('.temp');
const min = document.querySelector('.min');
const max = document.querySelector('.max');
const feels_deg = document.querySelector('.feels_deg');
const humidity_deg = document.querySelector('.humidity_deg');
const wind_speed = document.querySelector('.wind_speed');
const wind_pre = document.querySelector('.wind_pre');
const weather_body = document.querySelector('.weather_body');
//---------------------------------to get country name---------------------------------
const getCountryName = (Code) => {
return new Intl.DisplayNames([Code], { type: 'region' }).of(Code);
}
//------------------------------to get time and date-------------------------------
const getDateTime = (dt) => {
const currDate = new Date(dt * 1000);//conver second to millisecond
const option = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
};
return formatedDate = new Intl.DateTimeFormat(dt, option).format(currDate);
}
// -------------------------------to get city name--------------------------------
let city = "Delhi";
search.addEventListener('change', (e) => {
e.preventDefault();
city = search.value;
getWeatherData();
search.value = "";
})
//------------------------------to get weather data--------------------------------
const getWeatherData = async () => {
const weatherurl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=6d0d577998afe6f28d7e176702b3c53b`;
try {
const res = await fetch(weatherurl);
const data = await res.json();
const { main, name, weather, wind, sys, dt } = data;
country_inp.innerHTML = `${name},${getCountryName(sys.country)}`;
date_value.innerHTML = getDateTime(dt);
weather_feels.innerHTML = weather[0].description;
weather_png.innerHTML= `<img src="http://openweathermap.org/img/wn/${weather[0].icon}.png" alt="weather icon">`;
temp.innerHTML = `${Math.round(main.temp - 273)}<span>°C</span>`;
// temp.innerHTML = `${main.temp}°`;
min.innerHTML = `Min:${Math.round(main.temp_min - 273)}°`;
// min.innerHTML = `Min: ${main.temp_min}°`;
max.innerHTML = `Max:${Math.round((main.temp_max) - 273)}°`;
// max.innerHTML = `Max: ${main.temp_max}°`;
feels_deg.innerHTML=`${Math.round(main.feels_like - 273)}°`;
humidity_deg.innerHTML=`${main.humidity}%`;
wind_speed.innerHTML=`${wind.speed}m/s`;
wind_pre.innerHTML=`${main.pressure}hPa`;
} catch (error) {
weather_body.innerHTML=` ${error}`;
}
}
window.addEventListener("load", getWeatherData);