turn date into “2 days ago” format in javascript

There are various libraries available in JavaScript which can be used to display a date in the "X time ago" format. Two popular libraries are Moment.js and Timeago.js.

Using Moment.js:

index.tsx
var date = moment('2021-08-12T03:24:00');
var daysAgo = date.fromNow(); // "2 days ago"
88 chars
3 lines

Using Timeago.js:

index.tsx
var date = new Date('2021-08-12T03:24:00');
var daysAgo = timeago.format(date); // "2 days ago"
96 chars
3 lines

Both libraries allow you to format the date and time in a variety of ways, and also support internationalization.

gistlibby LogSnag