UNPKG

3.89 kBMarkdownView Raw
1# short-time-ago
2
3[![Build status](https://img.shields.io/github/workflow/status/velut/node-short-time-ago/CI)](https://github.com/velut/node-short-time-ago/actions?query=workflow%3ACI)
4[![Coverage](https://img.shields.io/codecov/c/gh/velut/node-short-time-ago)](https://codecov.io/gh/velut/node-short-time-ago)
5[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/short-time-ago)
6![Language](https://img.shields.io/github/languages/top/velut/node-short-time-ago)
7[![Dependencies](https://img.shields.io/david/velut/node-short-time-ago)](https://david-dm.org/velut/node-short-time-ago)
8[![npm bundle size](https://img.shields.io/bundlephobia/min/short-time-ago)](https://bundlephobia.com/result?p=short-time-ago)
9[![npm](https://img.shields.io/npm/v/short-time-ago)](https://www.npmjs.com/package/short-time-ago)
10[![License](https://img.shields.io/github/license/velut/node-short-time-ago)](https://github.com/velut/node-short-time-ago/blob/master/LICENSE)
11
12This package exports a single function, `timeAgo`,
13which describes the time elapsed between a given date and the current date
14in a human readable format (for example, _"10 minutes ago"_, _"in 3 seconds"_).
15
16## Pros
17
18- Simple API and usage
19- Small size (< 1KB)
20- No dependencies
21- Works in the browser
22- Written in Typescript
23- Well tested and documented
24- Accepts a custom date for the `now` anchor time
25
26## Cons
27
28- Only `en_US` locale support.
29
30## Contents
31
32View on [unpkg](https://unpkg.com/short-time-ago/).
33
34## Install
35
36Using `npm`:
37
38```
39npm i short-time-ago
40```
41
42Using `yarn`:
43
44```
45yarn add short-time-ago
46```
47
48## CDN
49
50UMD build:
51
52```html
53<!-- Add UMD script -->
54<script src="https://unpkg.com/short-time-ago"></script>
55
56<!-- Usage -->
57<script>
58 const timeAgo = window['short-time-ago'].timeAgo;
59
60 const myDate = new Date();
61 const description = timeAgo(myDate);
62
63 // Output: `just now`.
64 console.log(description);
65</script>
66```
67
68Module build:
69
70```html
71<script type="module">
72 import { timeAgo } from 'https://unpkg.com/short-time-ago?module';
73
74 const myDate = new Date();
75 const description = timeAgo(myDate);
76
77 // Output: `just now`.
78 console.log(description);
79</script>
80```
81
82## Usage
83
84Basic usage:
85
86```typescript
87import { timeAgo } from 'short-time-ago';
88
89const myDate = new Date();
90const description = timeAgo(myDate);
91
92// Output: `just now`.
93console.log(description);
94```
95
96Specifying a custom current date with the `now` parameter:
97
98```typescript
99import { timeAgo } from 'short-time-ago';
100
101const myDate = new Date('2019-01-01T00:00:00.000Z');
102const now = new Date('2019-01-01T00:01:00.000Z');
103const description = timeAgo(myDate, now);
104
105// Output: `1 minute ago`.
106console.log(description);
107```
108
109```typescript
110import { timeAgo } from 'short-time-ago';
111
112const myDate = new Date('2019-01-02T00:00:00.000Z');
113const now = new Date('2019-01-01T00:00:00.000Z');
114const description = timeAgo(myDate, now);
115
116// Output: `in 1 day`.
117console.log(description);
118```
119
120## API
121
122```typescript
123timeAgo: (date: Date, now?: Date) => string;
124```
125
126Explore the API on [jsDocs.io](https://www.jsdocs.io/package/short-time-ago).
127
128## Output
129
130The following table describes `timeAgo`'s output.
131
132| Time elapsed | Past output | Future output |
133| --------------------- | ----------------- | ---------------- |
134| < 1 second | `just now` | `just now` |
135| < 1 minute | `N second(s) ago` | `in N second(s)` |
136| < 1 hour | `N minute(s) ago` | `in N minute(s)` |
137| < 1 day | `N hour(s) ago` | `in N hour(s)` |
138| < 1 month (30.5 days) | `N day(s) ago` | `in N day(s)` |
139| < 1 year (365 days) | `N month(s) ago` | `in N month(s)` |
140| > 1 year | `N year(s) ago` | `in N year(s)` |
141
142## License
143
144MIT License
145
146Copyright (c) 2020 Edoardo Scibona
147
148See LICENSE file.