I am creating a comment section and want to add the time after it get posted into the user interface. Like "some seconds ago", the same as we see on Instagram or Facebook.
Asked
Active
Viewed 412 times
-2
-
https://i.stack.imgur.com/27m9E.jpg image link – AYUB KHAN May 01 '21 at 11:08
-
Does this answer your question? [How to format time since xxx e.g. “4 minutes ago” similar to Stack Exchange sites](https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site) – Ajeet Shah May 01 '21 at 11:41
2 Answers
0
add state in your comment component like timeSinceCommentCreation and on the instance when user post a comment, you need to update the state in componentWillUpdate equal to moment().startOf('hour').fromNow()
. use moment.js library which deals which is great to use time in your application

Aqib Naeem
- 431
- 3
- 11
0
First, you need to store the time when the comment is posted in the database. Like how you store comment content.
Second, in the front-end, you need to compare the posted time to the current time. If the difference is less than 60 seconds, show xx seconds ago, if above 60 seconds, you show xx minutes ago. Same for hours and days.

Xie Dongfeng
- 131
- 3