UNPKG

5.06 kBTypeScriptView Raw
1import type { ContextOptions, DateArg, MaybeArray } from "./types.js";
2/**
3 * The {@link intlFormatDistance} function options.
4 */
5export interface IntlFormatDistanceOptions
6 extends Intl.RelativeTimeFormatOptions,
7 ContextOptions<Date> {
8 /** Force the distance unit */
9 unit?: IntlFormatDistanceUnit;
10 /** The locales to use (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument) */
11 locale?: MaybeArray<Intl.ResolvedDateTimeFormatOptions["locale"]>;
12}
13/**
14 * The unit used to format the distance in {@link intlFormatDistance}.
15 */
16export type IntlFormatDistanceUnit =
17 | "year"
18 | "quarter"
19 | "month"
20 | "week"
21 | "day"
22 | "hour"
23 | "minute"
24 | "second";
25/**
26 * @name intlFormatDistance
27 * @category Common Helpers
28 * @summary Formats distance between two dates in a human-readable format
29 * @description
30 * The function calculates the difference between two dates and formats it as a human-readable string.
31 *
32 * The function will pick the most appropriate unit depending on the distance between dates. For example, if the distance is a few hours, it might return `x hours`. If the distance is a few months, it might return `x months`.
33 *
34 * You can also specify a unit to force using it regardless of the distance to get a result like `123456 hours`.
35 *
36 * See the table below for the unit picking logic:
37 *
38 * | Distance between dates | Result (past) | Result (future) |
39 * | ---------------------- | -------------- | --------------- |
40 * | 0 seconds | now | now |
41 * | 1-59 seconds | X seconds ago | in X seconds |
42 * | 1-59 minutes | X minutes ago | in X minutes |
43 * | 1-23 hours | X hours ago | in X hours |
44 * | 1 day | yesterday | tomorrow |
45 * | 2-6 days | X days ago | in X days |
46 * | 7 days | last week | next week |
47 * | 8 days-1 month | X weeks ago | in X weeks |
48 * | 1 month | last month | next month |
49 * | 2-3 months | X months ago | in X months |
50 * | 1 quarter | last quarter | next quarter |
51 * | 2-3 quarters | X quarters ago | in X quarters |
52 * | 1 year | last year | next year |
53 * | 2+ years | X years ago | in X years |
54 *
55 * @param laterDate - The date
56 * @param earlierDate - The date to compare with.
57 * @param options - An object with options.
58 * See MDN for details [Locale identification and negotiation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_identification_and_negotiation)
59 * The narrow one could be similar to the short one for some locales.
60 *
61 * @returns The distance in words according to language-sensitive relative time formatting.
62 *
63 * @throws `date` must not be Invalid Date
64 * @throws `baseDate` must not be Invalid Date
65 * @throws `options.unit` must not be invalid Unit
66 * @throws `options.locale` must not be invalid locale
67 * @throws `options.localeMatcher` must not be invalid localeMatcher
68 * @throws `options.numeric` must not be invalid numeric
69 * @throws `options.style` must not be invalid style
70 *
71 * @example
72 * // What is the distance between the dates when the fist date is after the second?
73 * intlFormatDistance(
74 * new Date(1986, 3, 4, 11, 30, 0),
75 * new Date(1986, 3, 4, 10, 30, 0)
76 * )
77 * //=> 'in 1 hour'
78 *
79 * // What is the distance between the dates when the fist date is before the second?
80 * intlFormatDistance(
81 * new Date(1986, 3, 4, 10, 30, 0),
82 * new Date(1986, 3, 4, 11, 30, 0)
83 * )
84 * //=> '1 hour ago'
85 *
86 * @example
87 * // Use the unit option to force the function to output the result in quarters. Without setting it, the example would return "next year"
88 * intlFormatDistance(
89 * new Date(1987, 6, 4, 10, 30, 0),
90 * new Date(1986, 3, 4, 10, 30, 0),
91 * { unit: 'quarter' }
92 * )
93 * //=> 'in 5 quarters'
94 *
95 * @example
96 * // Use the locale option to get the result in Spanish. Without setting it, the example would return "in 1 hour".
97 * intlFormatDistance(
98 * new Date(1986, 3, 4, 11, 30, 0),
99 * new Date(1986, 3, 4, 10, 30, 0),
100 * { locale: 'es' }
101 * )
102 * //=> 'dentro de 1 hora'
103 *
104 * @example
105 * // Use the numeric option to force the function to use numeric values. Without setting it, the example would return "tomorrow".
106 * intlFormatDistance(
107 * new Date(1986, 3, 5, 11, 30, 0),
108 * new Date(1986, 3, 4, 11, 30, 0),
109 * { numeric: 'always' }
110 * )
111 * //=> 'in 1 day'
112 *
113 * @example
114 * // Use the style option to force the function to use short values. Without setting it, the example would return "in 2 years".
115 * intlFormatDistance(
116 * new Date(1988, 3, 4, 11, 30, 0),
117 * new Date(1986, 3, 4, 11, 30, 0),
118 * { style: 'short' }
119 * )
120 * //=> 'in 2 yr'
121 */
122export declare function intlFormatDistance(
123 laterDate: DateArg<Date> & {},
124 earlierDate: DateArg<Date> & {},
125 options?: IntlFormatDistanceOptions,
126): string;