I would like to include very mon 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?
I would like to include very mon 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?
Share Improve this question asked Dec 16, 2010 at 12:59 mdrozdzielmdrozdziel 5,5586 gold badges40 silver badges55 bronze badges 3- 2 If the time is ing from the server, pay attention to issues like different time zones otherwise people will see "-2 hours ago". – Shadow Wizard Commented Dec 16, 2010 at 13:10
- Possible duplicate of stackoverflow./questions/2614032/… – El Ronnoco Commented Dec 16, 2010 at 13:20
- @Shadow Wizard For timeago (and I'd assume other similar libraries) as long as the date is UTC or contains the timezone it should use the timezone of the user (as reported by the browser). – Tim Lytle Commented Dec 16, 2010 at 14:00
2 Answers
Reset to default 12The 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.