I am prepend
ing a html document, so when user post a comment, it shows up in real time. Now the problem is the way the date is looking, i want the date to show in a format like this 3s ago
and not 09/08/2022 2:41 p.m.
, what is the best to achieve this
let _html = '
<div class="details_Comment">
<h4><b>{{c.user.username|title}}</b> <span>{% now "SHORT_DATETIME_FORMAT" %} ago</span></h4>
<p>' + _comment + '</p>
</div>
' $(".comment-wrapper").prepend(_html)
2
Answers
Here’s a working proof of concept, without using any external libraries. It basically just converts seconds to minutes if it’s over 60, minutes to hours if over 60, hours to days if over 24, etc…
You can now setInterval to say 1000ms to be updating the dates in them almost realistically.
If you have a very large data/comments, you may also want to kind of create a register for those that have already passed the parsing threshold e.g those comments that are more than one week old so that they don’t have to pass through this function agian (if you are a minimalist like me though)