-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
89 lines (61 loc) · 2.31 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const form = document.getElementById('form')
form.addEventListener('submit', work);
function work(e) {
e.preventDefault();
const givenLocation = document.getElementById('location').value
const temp = document.getElementById('temp');
const weatherIcon = document.getElementById('w-icon')
const weatherDesc = document.getElementById('w-desc')
const locationName = document.getElementById('location-name')
const localTime = document.getElementById('w-time')
const humidity = document.getElementById('w-humidity')
const pressure = document.getElementById('w-pressure')
const wind = document.getElementById('w-wind')
const URL = `https://api.weatherapi.com/v1/current.json?key=c5df16dfd56b45258d154259232006&q=${givenLocation}&aqi=no`
let data;
let xhr = new XMLHttpRequest();
xhr.open('GET', URL, true);
xhr.onload = function () {
if (this.status === 200) {
data = this.responseText;
data = JSON.parse(data)
/**
* Pushing Values to Frontend
*/
//Temperature
temp.innerHTML = data.current.temp_c + "°"
//Icon
weatherIcon.src = data.current.condition.icon
//Weather Description
weatherDesc.innerHTML = data.current.condition.text
//Location Name
locationName.innerHTML = data.location.name
//Time
let timeData = data.location.localtime
localTime.innerHTML = formatTime(timeData);
//Humidity
humidity.innerHTML = data.current.humidity + "%"
//Pressure
pressure.innerHTML = data.current.pressure_mb + 'mB'
//Wind
wind.innerHTML = data.current.wind_kph + ' km/h'
} else {
alert('Something Went Wrong')
}
}
xhr.send()
}
/**
* Function for formating time
*/
function formatTime(inputTime) {
let daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const time = new Date(inputTime);
daysOfWeek = daysOfWeek[time.getDay()]
formattedTime = new Intl.DateTimeFormat('en-us', {
hour: 'numeric',
minute: '2-digit',
hour12: true
}).format(time);
return `${daysOfWeek}, ${formattedTime}`
}