File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments