-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (37 loc) · 1.17 KB
/
index.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
firebase.database().ref('/news/').once('value').then(function(snapshot) {
data=snapshot.val();
if (data!=null) {
console.log("no news");
fillNews(data)
}
});
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear(),
hours = '' + d.getHours(),
minutes = '' + d.getMinutes();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
if (hours.length < 2)
hours = '0' + hours;
if (minutes.length < 2)
minutes = '0' + minutes;
return [year, month, day].join('-')+" "+hours + ":" + minutes;
}
function fillNews(data) {
var newsArray = Object.values(data);
newsArray = newsArray.sort(function(a ,b) {
return b.timestamp-a.timestamp;
})
console.log(newsArray);
const tableBody = document.getElementById('newsfeed');
let dataHtml = '';
newsArray.forEach(function(child) {
dataHtml += `<div class="news-post"><h3>${child.title}</h3><h5>${child.text}</h5><h6 class="news-timestamp">${formatDate(child.timestamp)}</h6></div>`;
});
tableBody.innerHTML = dataHtml;
}