-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
143 lines (100 loc) · 4.82 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function date() {
var now = new Date();
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
dates = [' ', '1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th', '12th', '13th', '14th', '15th', '16th', '17th', '18th', '19th', '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th', '29th', '30th', '31st', '32nd'];
weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
month = now.getMonth();
day = now.getDate();
week = now.getDay();
year = now.getFullYear();
document.getElementById('days').innerHTML = weekDays[week] + ', ' + months[month] + ' ' + dates[day] + ''+ ', ' + year;
}
date();
const btnCity = document.getElementById('addcity');
btnCity.addEventListener('click', addCity);
let city = document.getElementById('typedCity');
let cityTyped = city;
let cityWeather = document.getElementById('cityname');
function addCity() {
displayWeather(cityTyped.value);
}
let temparatureDescription = document.getElementById('temperature');
let tempIcon = document.getElementById('icons');
let tempDescription = document.getElementById('desc');
let sunSet = document.getElementById('sunset');
let sunRise = document.getElementById('sunrise');
function displayWeather() {
// BASED IN CITY NAME
// https://api.openweathermap.org/data/2.5/weather?q=${city}&appid={YOUR_API_KEY}
// BASED IN GEOLOCATION
// https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid={YOUR_API_KEY}
if (cityTyped.value != "") {
let city = cityTyped.value;
const api = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=fce8385e482d828553de63bf9408e19b`;
fetch(api)
.then(result => result.json())
.then(data => {
console.log(data);
const temperature = ((data.main.temp) - 273.15).toFixed(1);
const { description, icon } = data.weather[0];
const { sunrise, sunset } = data.sys;
let nameCity = data.name;
let cityData = data.sys.country;
let hourSunset = new Date(sunset * 1000).toLocaleTimeString();
let hourSunrise = new Date(sunrise * 1000).toLocaleTimeString();
temparatureDescription.innerText = `${temperature} ºC`;
tempDescription.innerText = description;
tempIcon.innerHTML = `<img src="http://openweathermap.org/img/wn/${icon}@2x.png">`;
sunSet.innerText = `Sunset: ${hourSunset}`;
sunRise.innerText = `Sunrise: ${hourSunrise}`;
cityWeather.innerText = `${nameCity}, ${cityData}`;
})
} else {
let lat;
let long;
navigator.geolocation.getCurrentPosition( position => {
long = position.coords.longitude;
lat = position.coords.latitude;
const apiGeo = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=fce8385e482d828553de63bf9408e19b`;
fetch(apiGeo)
.then(res => res.json())
.then(data => {
console.log(data);
const temperature = ((data.main.temp) - 273.15).toFixed(1);
const { description, icon } = data.weather[0];
const { sunrise, sunset } = data.sys;
let nameCity = data.name;
let cityData = data.sys.country;
let hourSunset = new Date(sunset * 1000).toLocaleTimeString();
let hourSunrise = new Date(sunrise * 1000).toLocaleTimeString();
temparatureDescription.innerText = `${temperature} ºC`;
tempDescription.innerText = description;
tempIcon.innerHTML = `<img src="http://openweathermap.org/img/wn/${icon}@2x.png">`;
sunSet.innerText = `Sunset: ${hourSunset}`;
sunRise.innerText = `Sunrise: ${hourSunrise}`;
cityWeather.innerText = `${nameCity}, ${cityData}`;
})
})
}
}
displayWeather();
// Clock
const clock = document.getElementById('watch');
function clockHour() {
let time = new Date();
let hour = time.getHours().toString();
let minutes = time.getMinutes().toString();
let seconds = time.getSeconds().toString();
if (hour.length < 2) {
hour = `0${hour}`;
}
if (minutes.length < 2) {
minutes = `0${minutes}`;
}
if (seconds.length < 2) {
seconds = `0${seconds}`;
}
let clockHourStr = `${hour} : ${minutes} . ${seconds}`;
clock.textContent = clockHourStr;
}
setInterval(clockHour, 1000);