The question is how to format a JavaScript Date
as a string stating the time elapsed similar to the way you see times displayed on Stack Overflow.
e.g.
- 1 minute ago
- 1 hour ago
- 1 day ago
- 1 month ago
- 1 year ago
The question is how to format a JavaScript Date
as a string stating the time elapsed similar to the way you see times displayed on Stack Overflow.
e.g.
Much readable and cross browser compatible code:
As given by @Travis
An ES6 version of the code provided by @user1012181
Edited with @ibe-vanmeenen suggestions. (Thanks !)
Simple and readable version:
I write one with js and python, used in two projects, very nice and simple: a simple library (less then 2kb) used to format date with
*** time ago
statement.simple, small, easy used, and well tested.
npm install timeago.js
import timeago from 'timeago.js'; // or use script tag
use api
format
.Sample:
Also you can render in real-time.
I haven't checked (although it wouldn't be hard to), but I think that Stack Exchange sites use the
jquery.timeago
plugin to create these time strings.It's quite easy to use the plugin, and it's clean and updates automatically.
Here's a quick sample (from the plugin's home page):