How to determine the distance in time in words in pure javascript?

I would like to include a very common feature in my web application. I would like time / dates to be written as the distance of time from now on in words, for example (2 hours ago).

Rails perfectly implements this functionality, but I need something like written in pure javascript. The problem is that many pages in my application are cached, and the only thing that will change is time.

Does JS have extended libraries that can translate a date to a distance of time in words?

+7
source share
2 answers

The timeago plugin for jQuery works well.

From the example code:

jQuery(document).ready(function() { jQuery("abbr.timeago").timeago(); }); 

Since you are referring to the use of libraries, I assume that "pure javascript" simply means nothing on the server.

+11
source

If you do not want to use jQuery, you can use JavaScript Relative Time Helpers . This is an extension to the Date class that provides a convenient toRelativeTime() method.

+4
source

All Articles