UNPKG

4.26 kBTypeScriptView Raw
1import type { ContextOptions, DateArg, LocalizedOptions } from "./types.js";
2/**
3 * The {@link formatDistance} function options.
4 */
5export interface FormatDistanceOptions
6 extends LocalizedOptions<"formatDistance">,
7 ContextOptions<Date> {
8 /** Distances less than a minute are more detailed */
9 includeSeconds?: boolean;
10 /** Add "X ago"/"in X" in the locale language */
11 addSuffix?: boolean;
12}
13/**
14 * @name formatDistance
15 * @category Common Helpers
16 * @summary Return the distance between the given dates in words.
17 *
18 * @description
19 * Return the distance between the given dates in words.
20 *
21 * | Distance between dates | Result |
22 * |-------------------------------------------------------------------|---------------------|
23 * | 0 ... 30 secs | less than a minute |
24 * | 30 secs ... 1 min 30 secs | 1 minute |
25 * | 1 min 30 secs ... 44 mins 30 secs | [2..44] minutes |
26 * | 44 mins ... 30 secs ... 89 mins 30 secs | about 1 hour |
27 * | 89 mins 30 secs ... 23 hrs 59 mins 30 secs | about [2..24] hours |
28 * | 23 hrs 59 mins 30 secs ... 41 hrs 59 mins 30 secs | 1 day |
29 * | 41 hrs 59 mins 30 secs ... 29 days 23 hrs 59 mins 30 secs | [2..30] days |
30 * | 29 days 23 hrs 59 mins 30 secs ... 44 days 23 hrs 59 mins 30 secs | about 1 month |
31 * | 44 days 23 hrs 59 mins 30 secs ... 59 days 23 hrs 59 mins 30 secs | about 2 months |
32 * | 59 days 23 hrs 59 mins 30 secs ... 1 yr | [2..12] months |
33 * | 1 yr ... 1 yr 3 months | about 1 year |
34 * | 1 yr 3 months ... 1 yr 9 month s | over 1 year |
35 * | 1 yr 9 months ... 2 yrs | almost 2 years |
36 * | N yrs ... N yrs 3 months | about N years |
37 * | N yrs 3 months ... N yrs 9 months | over N years |
38 * | N yrs 9 months ... N+1 yrs | almost N+1 years |
39 *
40 * With `options.includeSeconds == true`:
41 * | Distance between dates | Result |
42 * |------------------------|----------------------|
43 * | 0 secs ... 5 secs | less than 5 seconds |
44 * | 5 secs ... 10 secs | less than 10 seconds |
45 * | 10 secs ... 20 secs | less than 20 seconds |
46 * | 20 secs ... 40 secs | half a minute |
47 * | 40 secs ... 60 secs | less than a minute |
48 * | 60 secs ... 90 secs | 1 minute |
49 *
50 * @param laterDate - The date
51 * @param earlierDate - The date to compare with
52 * @param options - An object with options
53 *
54 * @returns The distance in words
55 *
56 * @throws `date` must not be Invalid Date
57 * @throws `baseDate` must not be Invalid Date
58 * @throws `options.locale` must contain `formatDistance` property
59 *
60 * @example
61 * // What is the distance between 2 July 2014 and 1 January 2015?
62 * const result = formatDistance(new Date(2014, 6, 2), new Date(2015, 0, 1))
63 * //=> '6 months'
64 *
65 * @example
66 * // What is the distance between 1 January 2015 00:00:15
67 * // and 1 January 2015 00:00:00, including seconds?
68 * const result = formatDistance(
69 * new Date(2015, 0, 1, 0, 0, 15),
70 * new Date(2015, 0, 1, 0, 0, 0),
71 * { includeSeconds: true }
72 * )
73 * //=> 'less than 20 seconds'
74 *
75 * @example
76 * // What is the distance from 1 January 2016
77 * // to 1 January 2015, with a suffix?
78 * const result = formatDistance(new Date(2015, 0, 1), new Date(2016, 0, 1), {
79 * addSuffix: true
80 * })
81 * //=> 'about 1 year ago'
82 *
83 * @example
84 * // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto?
85 * import { eoLocale } from 'date-fns/locale/eo'
86 * const result = formatDistance(new Date(2016, 7, 1), new Date(2015, 0, 1), {
87 * locale: eoLocale
88 * })
89 * //=> 'pli ol 1 jaro'
90 */
91export declare function formatDistance(
92 laterDate: DateArg<Date> & {},
93 earlierDate: DateArg<Date> & {},
94 options?: FormatDistanceOptions,
95): string;