UNPKG

2.31 kBTypeScriptView Raw
1/**
2 * This package exports a single function, {@link timeAgo},
3 * which describes the time elapsed between a given date and the current date
4 * in a human readable format (for example, `10 minutes ago`, `in 3 seconds`).
5 *
6 * @packageDocumentation
7 */
8/**
9 * `timeAgo` returns a string describing the time elapsed between
10 * a given date and the current time at which the function is called.
11 *
12 * @remarks
13 * `timeAgo` only supports the `en_US` locale.
14 *
15 * The following table describes `timeAgo`'s output.
16 *
17 * ```
18 * | Time elapsed | Past output | Future output |
19 * | --------------------- | ----------------- | ---------------- |
20 * | < 1 second | `just now` | `just now` |
21 * | < 1 minute | `N second(s) ago` | `in N second(s)` |
22 * | < 1 hour | `N minute(s) ago` | `in N minute(s)` |
23 * | < 1 day | `N hour(s) ago` | `in N hour(s)` |
24 * | < 1 month (30.5 days) | `N day(s) ago` | `in N day(s)` |
25 * | < 1 year (365 days) | `N month(s) ago` | `in N month(s)` |
26 * | > 1 year | `N year(s) ago` | `in N year(s)` |
27 * ```
28 *
29 * @example
30 * Basic usage:
31 *
32 * ```typescript
33 * import { timeAgo } from 'short-time-ago';
34 *
35 * const myDate = new Date();
36 * const description = timeAgo(myDate);
37 *
38 * // Output: `just now`.
39 * console.log(description);
40 * ```
41 *
42 * @example
43 * Specifying a custom current date with the `now` parameter:
44 *
45 * ```typescript
46 * import { timeAgo } from 'short-time-ago';
47 *
48 * const myDate = new Date('2019-01-01T00:00:00.000Z');
49 * const now = new Date('2019-01-01T00:01:00.000Z');
50 * const description = timeAgo(myDate, now);
51 *
52 * // Output: `1 minute ago`.
53 * console.log(description);
54 * ```
55 *
56 * ```typescript
57 * import { timeAgo } from 'short-time-ago';
58 *
59 * const myDate = new Date('2019-01-02T00:00:00.000Z');
60 * const now = new Date('2019-01-01T00:00:00.000Z');
61 * const description = timeAgo(myDate, now);
62 *
63 * // Output: `in 1 day`.
64 * console.log(description);
65 * ```
66 *
67 * @param date - a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date | Date}
68 * @param now - the current date (optional, defaults to `new Date()`)
69 */
70declare function timeAgo(date: Date, now?: Date): string;
71
72export { timeAgo };