UNPKG

2.79 kBJavaScriptView Raw
1"use strict";
2exports.formatDistanceToNowStrict = formatDistanceToNowStrict;
3var _index = require("./formatDistanceStrict.js");
4var _index2 = require("./constructNow.js");
5
6/**
7 * The {@link formatDistanceToNowStrict} function options.
8 */
9
10/**
11 * @name formatDistanceToNowStrict
12 * @category Common Helpers
13 * @summary Return the distance between the given date and now in words.
14 * @pure false
15 *
16 * @description
17 * Return the distance between the given dates in words, using strict units.
18 * This is like `formatDistance`, but does not use helpers like 'almost', 'over',
19 * 'less than' and the like.
20 *
21 * | Distance between dates | Result |
22 * |------------------------|---------------------|
23 * | 0 ... 59 secs | [0..59] seconds |
24 * | 1 ... 59 mins | [1..59] minutes |
25 * | 1 ... 23 hrs | [1..23] hours |
26 * | 1 ... 29 days | [1..29] days |
27 * | 1 ... 11 months | [1..11] months |
28 * | 1 ... N years | [1..N] years |
29 *
30 * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
31 *
32 * @param date - The given date
33 * @param options - An object with options.
34 *
35 * @returns The distance in words
36 *
37 * @throws `date` must not be Invalid Date
38 * @throws `options.locale` must contain `formatDistance` property
39 *
40 * @example
41 * // If today is 1 January 2015, what is the distance to 2 July 2014?
42 * const result = formatDistanceToNowStrict(
43 * new Date(2014, 6, 2)
44 * )
45 * //=> '6 months'
46 *
47 * @example
48 * // If now is 1 January 2015 00:00:00,
49 * // what is the distance to 1 January 2015 00:00:15, including seconds?
50 * const result = formatDistanceToNowStrict(
51 * new Date(2015, 0, 1, 0, 0, 15)
52 * )
53 * //=> '15 seconds'
54 *
55 * @example
56 * // If today is 1 January 2015,
57 * // what is the distance to 1 January 2016, with a suffix?
58 * const result = formatDistanceToNowStrict(
59 * new Date(2016, 0, 1),
60 * {addSuffix: true}
61 * )
62 * //=> 'in 1 year'
63 *
64 * @example
65 * // If today is 28 January 2015,
66 * // what is the distance to 1 January 2015, in months, rounded up??
67 * const result = formatDistanceToNowStrict(new Date(2015, 0, 1), {
68 * unit: 'month',
69 * roundingMethod: 'ceil'
70 * })
71 * //=> '1 month'
72 *
73 * @example
74 * // If today is 1 January 2015,
75 * // what is the distance to 1 January 2016 in Esperanto?
76 * const eoLocale = require('date-fns/locale/eo')
77 * const result = formatDistanceToNowStrict(
78 * new Date(2016, 0, 1),
79 * {locale: eoLocale}
80 * )
81 * //=> '1 jaro'
82 */
83function formatDistanceToNowStrict(date, options) {
84 return (0, _index.formatDistanceStrict)(
85 date,
86 (0, _index2.constructNow)(date),
87 options,
88 );
89}