js-ago

Simple time ago for UNIX timestamps and JavaScript Date objects.

Usage no npm install needed!

<script type="module">
  import jsAgo from 'https://cdn.skypack.dev/js-ago';
</script>

README

js-ago

Github issues GitHub stars GitHub license NPM version NPM downloads Twitter

Simple "time" ago for your Unix timestamps and JavaScript Date objects.

Installation

npm install js-ago

or

yarn add js-ago

Usage

The js_ago function accepts two arguments: js_ago(timestamp[, options]);

Parameter Required Type Default Possible Values
timestamp yes Date / Int A Date() object or an integer Unix timestamp
options no Object { format: "medium" } An object with the format property set as either "short", "medium" or "long"
import js_ago from 'js-ago';
// or
// const js_ago = require('js_ago');

js_ago(new Date('2020-10-17')); // 4 months ago

js_ago(1611344957); // 7 secs ago
js_ago(1611344957, { format: 'short' }); // 7s ago
js_ago(1611344957, { format: 'medium' }); // 7 secs ago
js_ago(1611344957, { format: 'long' }); // 7 seconds ago

In a React component:

import React from 'react';
import js_ago from 'js-ago';

export default function Article() {
    const timestamp = 1591872078; // E.g. fetched from an API

    return (
        <article>
            <h1>Post Title</h1>
            <p>Lorem ipsum...</p>
            <footer>Posted {js_ago(timestamp)}</footer>
            {/* Output: Posted 10 mins ago */}
        </article>
    );
}

Outputs

As of version 1.1.0, you can set the format property of the options passed to the function to determine the output format.

short medium (default) long
s sec second
m min minute
h hr hour
d day day
w wk week
m mon month
y yr year