开发者

How to define distance in time in words in pure javascript?

I would l开发者_如何学Goike to include very common feature in my web application. I would like the times/dates be written as the distance of time from now in words, for example (2 hours ago).

Rails have excellent implementation of this functionality, but I need something like that written in pure javascript. The problem is, that lots of pages in my application are cached, and the only thing that would change is the actual time.

Are there any extended libraries in JS which could translate a date to a distance of time in words?


The timeago plugin for jQuery works well.

From the example code:

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

Since you referenced using libraries, I'm assuming 'pure javascript' just means not anything on the server.


If you don't want to use jQuery, you can use JavaScript Relative Time Helpers. It's an extension of the Date class, providing a handy toRelativeTime() method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜