|
| 1 | +let cityname = document.querySelector(".weather_city"); |
| 2 | +let datetime = document.querySelector(".weather_date_time"); |
| 3 | +let w_forecast = document.querySelector(".weather_forecast"); |
| 4 | +let w_icon = document.querySelector(".weather_icon"); |
| 5 | +let w_temperature = document.querySelector(".weather_temperature"); |
| 6 | +let w_minterm = document.querySelector(".weather_min"); |
| 7 | +let w_maxterm = document.querySelector(".weather_max"); |
| 8 | + |
| 9 | +let w_feelslike = document.querySelector(".weather_feelslike"); |
| 10 | +let w_humidity = document.querySelector(".weather_humidity"); |
| 11 | +let w_wind = document.querySelector(".weather_wind"); |
| 12 | +let w_pressure = document.querySelector(".weather_pressure"); |
| 13 | + |
| 14 | +let citySearch = document.querySelector(".weather_search"); |
| 15 | + |
| 16 | + |
| 17 | +// full country name using the of method |
| 18 | +const getcountryName = (code) => { |
| 19 | + return new Intl.DisplayNames([code], { type: "region" }).of(code); |
| 20 | +}; |
| 21 | + |
| 22 | +// created the date and dtime |
| 23 | +const getDateTime = (dt) => { |
| 24 | + //let dt = 1729699653; |
| 25 | + const currdate = new Date(dt * 1000); |
| 26 | + console.log(currdate); |
| 27 | + |
| 28 | + const options = { |
| 29 | + weekday: "long", |
| 30 | + year: "numeric", |
| 31 | + month: "long", |
| 32 | + day: "numeric", |
| 33 | + hour: "numeric", |
| 34 | + minute: "numeric" |
| 35 | + }; |
| 36 | + const formatter = new Intl.DateTimeFormat("en-US", options); |
| 37 | + console.log(formatter); |
| 38 | + return formatter.format(currdate); |
| 39 | +}; |
| 40 | + |
| 41 | +let city = "pune"; |
| 42 | + |
| 43 | +// searching the city |
| 44 | +citySearch.addEventListener('submit',(e)=>{ |
| 45 | + let cityname = document.querySelector(".city_name"); |
| 46 | + e.preventDefault(); |
| 47 | + console.log(cityname.value); |
| 48 | + city = cityname.value; |
| 49 | + // this is using for call function |
| 50 | + getweatherData(); |
| 51 | + // this is using for cleaning the function |
| 52 | + cityname.value = ""; |
| 53 | +}); |
| 54 | + |
| 55 | + |
| 56 | +const getweatherData = async () => { |
| 57 | + //const weatherUrl = `https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid=daa5c17ee64f6aca6c86e7986941e76c`; |
| 58 | + const weatherUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=a0b06669a793506ae54060f7df8a5fb6`; |
| 59 | + |
| 60 | + try { |
| 61 | + const res = await fetch(weatherUrl); |
| 62 | + const data = await res.json(); |
| 63 | + console.log(data); |
| 64 | + |
| 65 | + const { main, name, weather, wind, sys, dt } = data; |
| 66 | + |
| 67 | + cityname.innerHTML = `${name} , ${getcountryName(sys.country)} `; |
| 68 | + datetime.innerHTML = getDateTime(dt); |
| 69 | + |
| 70 | + |
| 71 | + w_forecast.innerHTML = weather[0].main; |
| 72 | + w_icon.innerHTML = `<img src="http://openweathermap.org/img/wn/${weather[0].icon}@2x.png" alt="Weather Icon">`; |
| 73 | + |
| 74 | + w_temperature.innerHTML = `Max:${main.temp}°`; |
| 75 | + w_minterm.innerHTML = `Min:${main.temp_min.toFixed()}°`; |
| 76 | + // w_maxterm.innerHTML = `${main.temp_max.toFixed()}°`; |
| 77 | + |
| 78 | + w_feelslike.innerHTML = `${main.feels_like.toFixed(2)} K`; |
| 79 | + w_humidity.innerHTML = `${main.humidity} %`; |
| 80 | + w_wind.innerHTML = `${wind.speed} m/s`; |
| 81 | + w_pressure.innerHTML = `${main.pressure} hpa`; |
| 82 | + |
| 83 | + } catch (error) { |
| 84 | + console.log("error"); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +document.body.addEventListener("load", getweatherData()); |
0 commit comments