Epoch TimeAgo

Convert unix time to a relative time string e.g., 4 hours ago.

As the name implies, only epoch time (unix time) can be used. The output format is similar to what you would find on Reddit.

There is zero configuration. Zero dependencies. Minifies to ~700 bytes, and gzips to under 370 bytes. 😎

Examples:

  • seconds > just now
  • minutes > a minute ago, 15 minutes ago
  • hours > an hour ago, 2 hours ago
  • days > 1 day ago, 2 days ago
  • weeks > 1 week ago, 3 weeks ago
  • months > 1 month ago, 5 months ago
  • years > 1 year ago, 2 years ago

Install

npm install -S epoch-timeago

Usage

Simply pass a unix timestamp, and get a time ago formated string.

import timeAgo form 'epoch-timeago';

const timeSince = timeAgo(Date.now() - 60000 * 10);
// '10 minutes ago'

In React

import React from 'react';
import timeAgo form 'epoch-timeago';

const TimeAgo = ({ time }) =>
  <time datetime={new Date(time).toISOString()}>{timeAgo(time)}</time>

...

const epochTimeStamp = Date.now() - 60000 * 22;
<TimeAgo time={epochTimeStamp} />

License MIT

Built with Compositor.io