2.1.1 • Published 3 months ago

js-ago v2.1.1

Weekly downloads
13
License
MIT
Repository
github
Last release
3 months ago

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

or

pnpm add js-ago

Usage

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

ParameterRequiredTypeDefaultPossible Values
timestampyesDate / IntA Date() object or an integer Unix timestamp
optionsnoObject{ 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.

shortmedium (default)long
ssecsecond
mminminute
hhrhour
ddayday
wwkweek
mmonmonth
yyryear

Naming convention

Although the conventional naming in JS is camelCase, due to historical reasons, the function name is js_ago instead of jsAgo 👴

You can rename the method when importing it:

import jsAgo from "js-ago";