-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (49 loc) · 1.78 KB
/
index.js
File metadata and controls
63 lines (49 loc) · 1.78 KB
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
function createPost(post) {
const cl = document.getElementById("content-list");
const p = document.createElement("li");
const title = document.createElement("a");
const desc = document.createElement("a");
const time = document.createElement("time");
title.className = "post_title";
title.innerHTML = `${post.title}`;
title.href = `${post.url}`;
desc.className = "post_desc";
desc.innerHTML = `${post.description}`;
desc.href = `${post.url}`;
date = new Date(post.date_created);
date_edited = new Date(post.date_last_edited)
time.className = "post_time"
time.dateTime = date.toString();
time.innerHTML = formatDate(date);
p.classList.add("post");
p.appendChild(title);
p.appendChild(desc);
p.appendChild(time);
cl.appendChild(p);
}
function createPosts() {
fetch('posts.json')
.then(response => response.json())
.then(posts => {
posts.forEach(post => {
createPost(post);
});
});
}
function formatDate(date) {
const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
let day = days[date.getDay()];
let month = months[date.getMonth()];
let dateOfMonth = date.getDate();
let year = date.getFullYear();
let hours = date.getHours();
let minutes = date.getMinutes();
let ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
let timezone = "EST"; // Static timezone, adjust as needed
return `${day} ${month} ${dateOfMonth} ${year} ${hours}:${minutes} ${ampm} ${timezone}`;
}
createPosts();