-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (30 loc) · 1.32 KB
/
script.js
File metadata and controls
40 lines (30 loc) · 1.32 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
let search_btn = document.getElementById("search_btn");
function currentTime(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
document.getElementById("displayCurrentTime").innerHTML = currentTime(new Date());
function search(){
let inputState = document.getElementById("searchState").value
let displayCityName = document.getElementById("displayCityName")
let key =
`https://api.openweathermap.org/data/2.5/weather?q=${inputState}&appid=84a5541e536549f59b50ed907cab345b&units=metric`;
fetch(key)
.then((res) => res.json())
.then((data)=>{
console.log(data)
// console.log(data.weather[0])
description.innerHTML = data.weather[0].main
description.innerHTML = data.weather[0].description
displayCityTemparature.innerHTML= Math.round(data.main.temp) +`°C`
displayCityName.innerHTML = `${data.name},${data.sys.country}`
})
.catch((err) =>displayCityName.innerHTML= `<p>Enter a valid city name</p>`);
}
search_btn.addEventListener("click",search)