index.js
9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import compareAsc from '../compareAsc/index.js';
import differenceInMonths from '../differenceInMonths/index.js';
import differenceInSeconds from '../differenceInSeconds/index.js';
import defaultLocale from '../locale/en-US/index.js';
import toDate from '../toDate/index.js';
import cloneObject from '../_lib/cloneObject/index.js';
import getTimezoneOffsetInMilliseconds from '../_lib/getTimezoneOffsetInMilliseconds/index.js';
import requiredArgs from '../_lib/requiredArgs/index.js';
var MINUTES_IN_DAY = 1440;
var MINUTES_IN_ALMOST_TWO_DAYS = 2520;
var MINUTES_IN_MONTH = 43200;
var MINUTES_IN_TWO_MONTHS = 86400;
/**
* @name formatDistance
* @category Common Helpers
* @summary Return the distance between the given dates in words.
*
* @description
* Return the distance between the given dates in words.
*
* | Distance between dates | Result |
* |-------------------------------------------------------------------|---------------------|
* | 0 ... 30 secs | less than a minute |
* | 30 secs ... 1 min 30 secs | 1 minute |
* | 1 min 30 secs ... 44 mins 30 secs | [2..44] minutes |
* | 44 mins ... 30 secs ... 89 mins 30 secs | about 1 hour |
* | 89 mins 30 secs ... 23 hrs 59 mins 30 secs | about [2..24] hours |
* | 23 hrs 59 mins 30 secs ... 41 hrs 59 mins 30 secs | 1 day |
* | 41 hrs 59 mins 30 secs ... 29 days 23 hrs 59 mins 30 secs | [2..30] days |
* | 29 days 23 hrs 59 mins 30 secs ... 44 days 23 hrs 59 mins 30 secs | about 1 month |
* | 44 days 23 hrs 59 mins 30 secs ... 59 days 23 hrs 59 mins 30 secs | about 2 months |
* | 59 days 23 hrs 59 mins 30 secs ... 1 yr | [2..12] months |
* | 1 yr ... 1 yr 3 months | about 1 year |
* | 1 yr 3 months ... 1 yr 9 month s | over 1 year |
* | 1 yr 9 months ... 2 yrs | almost 2 years |
* | N yrs ... N yrs 3 months | about N years |
* | N yrs 3 months ... N yrs 9 months | over N years |
* | N yrs 9 months ... N+1 yrs | almost N+1 years |
*
* With `options.includeSeconds == true`:
* | Distance between dates | Result |
* |------------------------|----------------------|
* | 0 secs ... 5 secs | less than 5 seconds |
* | 5 secs ... 10 secs | less than 10 seconds |
* | 10 secs ... 20 secs | less than 20 seconds |
* | 20 secs ... 40 secs | half a minute |
* | 40 secs ... 60 secs | less than a minute |
* | 60 secs ... 90 secs | 1 minute |
*
* ### v2.0.0 breaking changes:
*
* - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
*
* - The function was renamed from `distanceInWords ` to `formatDistance`
* to make its name consistent with `format` and `formatRelative`.
*
* - The order of arguments is swapped to make the function
* consistent with `differenceIn...` functions.
*
* ```javascript
* // Before v2.0.0
*
* distanceInWords(
* new Date(1986, 3, 4, 10, 32, 0),
* new Date(1986, 3, 4, 11, 32, 0),
* { addSuffix: true }
* ) //=> 'in about 1 hour'
*
* // v2.0.0 onward
*
* formatDistance(
* new Date(1986, 3, 4, 11, 32, 0),
* new Date(1986, 3, 4, 10, 32, 0),
* { addSuffix: true }
* ) //=> 'in about 1 hour'
* ```
*
* @param {Date|Number} date - the date
* @param {Date|Number} baseDate - the date to compare with
* @param {Object} [options] - an object with options.
* @param {Boolean} [options.includeSeconds=false] - distances less than a minute are more detailed
* @param {Boolean} [options.addSuffix=false] - result indicates if the second date is earlier or later than the first
* @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
* @returns {String} the distance in words
* @throws {TypeError} 2 arguments required
* @throws {RangeError} `date` must not be Invalid Date
* @throws {RangeError} `baseDate` must not be Invalid Date
* @throws {RangeError} `options.locale` must contain `formatDistance` property
*
* @example
* // What is the distance between 2 July 2014 and 1 January 2015?
* var result = formatDistance(new Date(2014, 6, 2), new Date(2015, 0, 1))
* //=> '6 months'
*
* @example
* // What is the distance between 1 January 2015 00:00:15
* // and 1 January 2015 00:00:00, including seconds?
* var result = formatDistance(
* new Date(2015, 0, 1, 0, 0, 15),
* new Date(2015, 0, 1, 0, 0, 0),
* { includeSeconds: true }
* )
* //=> 'less than 20 seconds'
*
* @example
* // What is the distance from 1 January 2016
* // to 1 January 2015, with a suffix?
* var result = formatDistance(new Date(2015, 0, 1), new Date(2016, 0, 1), {
* addSuffix: true
* })
* //=> 'about 1 year ago'
*
* @example
* // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto?
* import { eoLocale } from 'date-fns/locale/eo'
* var result = formatDistance(new Date(2016, 7, 1), new Date(2015, 0, 1), {
* locale: eoLocale
* })
* //=> 'pli ol 1 jaro'
*/
export default function formatDistance(dirtyDate, dirtyBaseDate, dirtyOptions) {
requiredArgs(2, arguments);
var options = dirtyOptions || {};
var locale = options.locale || defaultLocale;
if (!locale.formatDistance) {
throw new RangeError('locale must contain formatDistance property');
}
var comparison = compareAsc(dirtyDate, dirtyBaseDate);
if (isNaN(comparison)) {
throw new RangeError('Invalid time value');
}
var localizeOptions = cloneObject(options);
localizeOptions.addSuffix = Boolean(options.addSuffix);
localizeOptions.comparison = comparison;
var dateLeft;
var dateRight;
if (comparison > 0) {
dateLeft = toDate(dirtyBaseDate);
dateRight = toDate(dirtyDate);
} else {
dateLeft = toDate(dirtyDate);
dateRight = toDate(dirtyBaseDate);
}
var seconds = differenceInSeconds(dateRight, dateLeft);
var offsetInSeconds = (getTimezoneOffsetInMilliseconds(dateRight) - getTimezoneOffsetInMilliseconds(dateLeft)) / 1000;
var minutes = Math.round((seconds - offsetInSeconds) / 60);
var months; // 0 up to 2 mins
if (minutes < 2) {
if (options.includeSeconds) {
if (seconds < 5) {
return locale.formatDistance('lessThanXSeconds', 5, localizeOptions);
} else if (seconds < 10) {
return locale.formatDistance('lessThanXSeconds', 10, localizeOptions);
} else if (seconds < 20) {
return locale.formatDistance('lessThanXSeconds', 20, localizeOptions);
} else if (seconds < 40) {
return locale.formatDistance('halfAMinute', null, localizeOptions);
} else if (seconds < 60) {
return locale.formatDistance('lessThanXMinutes', 1, localizeOptions);
} else {
return locale.formatDistance('xMinutes', 1, localizeOptions);
}
} else {
if (minutes === 0) {
return locale.formatDistance('lessThanXMinutes', 1, localizeOptions);
} else {
return locale.formatDistance('xMinutes', minutes, localizeOptions);
}
} // 2 mins up to 0.75 hrs
} else if (minutes < 45) {
return locale.formatDistance('xMinutes', minutes, localizeOptions); // 0.75 hrs up to 1.5 hrs
} else if (minutes < 90) {
return locale.formatDistance('aboutXHours', 1, localizeOptions); // 1.5 hrs up to 24 hrs
} else if (minutes < MINUTES_IN_DAY) {
var hours = Math.round(minutes / 60);
return locale.formatDistance('aboutXHours', hours, localizeOptions); // 1 day up to 1.75 days
} else if (minutes < MINUTES_IN_ALMOST_TWO_DAYS) {
return locale.formatDistance('xDays', 1, localizeOptions); // 1.75 days up to 30 days
} else if (minutes < MINUTES_IN_MONTH) {
var days = Math.round(minutes / MINUTES_IN_DAY);
return locale.formatDistance('xDays', days, localizeOptions); // 1 month up to 2 months
} else if (minutes < MINUTES_IN_TWO_MONTHS) {
months = Math.round(minutes / MINUTES_IN_MONTH);
return locale.formatDistance('aboutXMonths', months, localizeOptions);
}
months = differenceInMonths(dateRight, dateLeft); // 2 months up to 12 months
if (months < 12) {
var nearestMonth = Math.round(minutes / MINUTES_IN_MONTH);
return locale.formatDistance('xMonths', nearestMonth, localizeOptions); // 1 year up to max Date
} else {
var monthsSinceStartOfYear = months % 12;
var years = Math.floor(months / 12); // N years up to 1 years 3 months
if (monthsSinceStartOfYear < 3) {
return locale.formatDistance('aboutXYears', years, localizeOptions); // N years 3 months up to N years 9 months
} else if (monthsSinceStartOfYear < 9) {
return locale.formatDistance('overXYears', years, localizeOptions); // N years 9 months up to N year 12 months
} else {
return locale.formatDistance('almostXYears', years + 1, localizeOptions);
}
}
}