Coder Social home page Coder Social logo

js-ago's Introduction

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]);

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

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";

js-ago's People

Contributors

arvin7 avatar dependabot[bot] avatar

Stargazers

Dids Irwyn Reyes avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.