-
Notifications
You must be signed in to change notification settings - Fork 1
/
recentQuakes.js
64 lines (62 loc) · 2.2 KB
/
recentQuakes.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
function recentQuakes() {
// Date formatting
const date = new Date();
const year = date.getFullYear();
let monthVal = date.getMonth();
const monthMap = {
0: "01",
1: "02",
2: "03",
3: "04",
4: "05",
5: "06",
6: "07",
7: "08",
8: "09",
9: "10",
10: "11",
11: "12",
};
const month = monthMap[monthVal];
let day = date.getDate();
if (day < 10) {
day = "0" + day;
}
const startDate = `${year}-${month}-${day}`;
const nextMonth = monthMap[monthVal + 1];
const endDate = `${year}-${nextMonth}-${day}`;
// USGS Earthquake API
fetch(
`https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=${startDate}&endtime=${endDate}&minmagnitude=2`
)
.then(function (response) {
return response.json();
})
.then(function (data) {
const dataArray = data.features;
const mostRecent = document.getElementById("mostRecent");
dataArray.map(function (earthquake) {
const eachQuake = earthquake;
const utcMilliSeconds = eachQuake.properties.time;
const seconds = Math.round(utcMilliSeconds / 1000);
const theTime = new Date(0);
theTime.setUTCSeconds(seconds);
const stringTime = theTime.toString();
const splitStringTime = stringTime.split(" ");
const newTime = `${splitStringTime[0]} ${splitStringTime[1]} ${splitStringTime[2]} ${splitStringTime[3]} ${splitStringTime[4]}`;
mostRecent.innerHTML += `
<div class="terms">
<hr></hr>
<h4>${eachQuake.properties.title}</h4>
<div>📈 <h7>${eachQuake.properties.mag}</h7></div>
<div>🌍 <a href="https://www.openstreetmap.org/#map=10/${eachQuake.geometry.coordinates[1]}/${eachQuake.geometry.coordinates[0]}" target="_blank">${eachQuake.properties.place}</a></div>
<div>🕔 <h7>${newTime}</h7></div>
<br></br>
<a href=${eachQuake.properties.url} target="_blank">See more detailed information about this earthquake on the USGS website.</a>
<br></br>
</div>
`;
});
});
}
getRecent.addEventListener("click", recentQuakes);