Skip to content

$.time_ago()

Arthur Guiot edited this page Aug 10, 2017 · 1 revision

How does it work?

Do you have a web app with "time-related" elements (like blog posts or objects in a timeline)?

If yes, this funtion is for you 😊!

Basically, this function will from a given time generate a string like:

  • 2 days ago
  • Yesterday
  • 10 minutes from now
  • ...

How to use it?

This is very simple, you only need to give it a new Date() object.

Example

JS:

var aDay = 24 * 60 * 60 * 1000;
console.log($.time_ago(new Date(Date.now() - aDay))); // Yesterday
console.log($.time_ago(new Date(Date.now() - aDay * 2))); // 2 days ago

You even can use the $.dynamic() function with it to have a realtime render.

Clone this wiki locally