2.1.1 • Published 2 years ago
js-ago v2.1.1
js-ago
Simple "time" ago for your Unix timestamps and JavaScript Date objects.
Installation
npm install js-agoor
yarn add js-agoor
pnpm add js-agoUsage
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 agoIn 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 |
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";