Skip to content

Commit 7545aea

Browse files
committed
Update comments.js
1 parent 9bdd1a8 commit 7545aea

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

posts/js/comments.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,33 @@ document.addEventListener("DOMContentLoaded", () => {
1515
const p = document.createElement("p");
1616
const author = sanitize(comment.author || "Anonymous");
1717
const text = sanitize(comment.text);
18-
p.innerHTML = `<strong>${author}:</strong> ${text}`;
18+
19+
let timeText = "";
20+
if (comment.timestamp) {
21+
const date = new Date(comment.timestamp);
22+
const now = new Date();
23+
const diff = Math.floor((now - date) / 1000);
24+
if (diff < 60) {
25+
timeText = "just now";
26+
} else if (diff < 3600) {
27+
timeText = `${Math.floor(diff / 60)} minutes ago`;
28+
} else if (diff < 86400) {
29+
timeText = `${Math.floor(diff / 3600)} hours ago`;
30+
} else {
31+
timeText = `${Math.floor(diff / 86400)} days ago`;
32+
}
33+
}
34+
if (timeText) {
35+
const timeEl = document.createElement("span");
36+
timeEl.className = "comment-time";
37+
timeEl.textContent = ` (${timeText})`;
38+
p.appendChild(timeEl);
39+
}
40+
41+
p.innerHTML = `
42+
<strong>${author}:</strong> ${text}
43+
<span class="comment-time">${timeText}</span>
44+
`;
1945
commentsList.appendChild(p);
2046
}
2147

0 commit comments

Comments
 (0)