-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
71 lines (61 loc) · 2.07 KB
/
script.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
let now = new Date();
let h2 = document.querySelector("h2");
let hours = now.getHours();
let minutes = now.getMinutes();
let year = now.getFullYear();
const currentDate = new Date();
const formattedTime = new Intl.DateTimeFormat("en-GB", {
hour: "numeric",
minute: "numeric",
hour12: false,
}).format(currentDate);
console.log(formattedTime);
let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
let day = days[now.getDay()];
let months = [
"Jan",
"Feb",
"March",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
let month = months[now.getMonth()];
h2.innerHTML = `${day} ${currentDate.getDate()} ${month}, ${hours}:${minutes}, ${year}`;
function searchCity(event) {
event.preventDefault();
let searchInput = document.querySelector("#search-text-input");
let h1 = document.querySelector("h1");
h1.innerHTML = searchInput.value;
search(searchInput.value);
}
function search(city) {
let apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=f09d3949047ab6c9e3bcaf79cf61f619`;
axios.get(apiUrl).then(showTemperature);
}
let form = document.querySelector("form");
form.addEventListener("submit", searchCity);
function showTemperature(response) {
let temperature = Math.round(response.data.main.temp);
let temperatureElement = document.querySelector("#weather");
let description = document.querySelector("#description");
temperatureElement.innerHTML = `${temperature}°C`;
description.innerHTML = response.data.weather[0].description;
}
function searchLocation(position) {
let apiUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${position.coords.latitude}&lon=${position.coords.longitude}&appid=f09d3949047ab6c9e3bcaf79cf61f619&units=metric`;
axios.get(apiUrl).then(showTemperature);
console.log(apiUrl);
}
function getCurrentLocation(event) {
event.preventDefault();
navigator.geolocation.getCurrentPosition(searchLocation);
}
let currentLocationButton = document.querySelector("#current-location-button");
currentLocationButton.addEventListener("click", getCurrentLocation);