short-time-ago

  • Version 2.0.0
  • Published
  • 29.4 kB
  • No dependencies
  • MIT license

Install

npm i short-time-ago
yarn add short-time-ago
pnpm add short-time-ago

Overview

This package exports a single function, timeAgo, which describes the time elapsed between a given date and the current date in a human readable format (for example, 10 minutes ago, in 3 seconds).

Index

Functions

Functions

function timeAgo

timeAgo: (date: Date, now?: Date) => string;
  • timeAgo returns a string describing the time elapsed between a given date and the current time at which the function is called.

    Parameter date

    a Date

    Parameter now

    the current date (optional, defaults to new Date())

    Remarks

    timeAgo only supports the en_US locale.

    The following table describes timeAgo's output.

    | Time elapsed | Past output | Future output |
    | --------------------- | ----------------- | ---------------- |
    | < 1 second | `just now` | `just now` |
    | < 1 minute | `N second(s) ago` | `in N second(s)` |
    | < 1 hour | `N minute(s) ago` | `in N minute(s)` |
    | < 1 day | `N hour(s) ago` | `in N hour(s)` |
    | < 1 month (30.5 days) | `N day(s) ago` | `in N day(s)` |
    | < 1 year (365 days) | `N month(s) ago` | `in N month(s)` |
    | > 1 year | `N year(s) ago` | `in N year(s)` |

    Example 1

    Basic usage:

    import { timeAgo } from 'short-time-ago';
    const myDate = new Date();
    const description = timeAgo(myDate);
    // Output: `just now`.
    console.log(description);

    Example 2

    Specifying a custom current date with the now parameter:

    import { timeAgo } from 'short-time-ago';
    const myDate = new Date('2019-01-01T00:00:00.000Z');
    const now = new Date('2019-01-01T00:01:00.000Z');
    const description = timeAgo(myDate, now);
    // Output: `1 minute ago`.
    console.log(description);

    import { timeAgo } from 'short-time-ago';
    const myDate = new Date('2019-01-02T00:00:00.000Z');
    const now = new Date('2019-01-01T00:00:00.000Z');
    const description = timeAgo(myDate, now);
    // Output: `in 1 day`.
    console.log(description);

Package Files (1)

Dependencies (0)

No dependencies.

Dev Dependencies (9)

Peer Dependencies (0)

No peer dependencies.

Badge

To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/short-time-ago.

  • Markdown
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/short-time-ago)
  • HTML
    <a href="https://www.jsdocs.io/package/short-time-ago"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>