1

I'm making a simple website, in this website when I make a post or comment, I want it to display how old this comment is.

Example1: "I like apples" Added 10 days ago // This is the time, it should start at 0 seconds, when published it should then count.

Example2: "I like bananas" Added: 1 hour 3 minutes ago

Something in html would be good, for example I add a line such as ((Date xx - Date xx) start counting seconds) Once I publish this piece of text then it counts and i get "Added: 10 days ago".

Hope that was clear enough and thanks. I don't mind a solution in Java either as long as it's easy to copy paste since I'll need to make a lot of "comments".

3 Answers3

6

You can use relative time functionality from moment.js

gvlasov
  • 18,638
  • 21
  • 74
  • 110
3

You may find this plugin useful.

Eduard
  • 3,395
  • 8
  • 37
  • 62
2

This is not possible in HTML. You need a programming language. And you should handle this in adding comments; you need to store information about the actual change, either manually (if you edit pages manually) or in the the software that adds the comments, such as PHP code or a CMS. To calculate difference between that moment of time and the current time, you need to decide on “current time”. You will probably end up with a decision to use the time that the user is viewing the page, and this will lead you to using JavaScript. It will be rather easy as soon as you have actually stored the addition time in the HTML document (e.g., as visible date or invisibly in an attribute), but it will of course depend on the format used for that time. In any case, it should be either universal time or local time together with a time zone designation; otherwise you cannot calculate correctly the difference.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390