/
index.js
86 lines (76 loc) · 3.18 KB
/
index.js
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
import getTimezoneOffsetInMilliseconds from '../_lib/getTimezoneOffsetInMilliseconds/index.js'
import toDate from '../toDate/index.js'
import format from '../format/index.js'
import differenceInCalendarDays from '../differenceInCalendarDays/index.js'
import defaultLocale from '../locale/en-US/index.js'
import subMilliseconds from '../subMilliseconds/index.js'
/**
* @name formatRelative
* @category Common Helpers
* @summary Represent the date in words relative to the given base date.
*
* @description
* Represent the date in words relative to the given base date.
*
* | Distance to the base date | Result |
* |---------------------------|---------------------------|
* | Previous 6 days | last Sunday at 04:30 AM |
* | Last day | yesterday at 04:30 AM |
* | Same day | today at 04:30 AM |
* | Next day | tomorrow at 04:30 AM |
* | Next 6 days | Sunday at 04:30 AM |
* | Other | 12/31/2017 |
*
* @param {Date|String|Number} date - the date to format
* @param {Date|String|Number} baseDate - the date to compare with
* @param {Options} [options] - the object with options. See [Options]{@link https://date-fns.org/docs/Options}
* @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
* @returns {String} the date in words
* @throws {TypeError} 2 arguments required
* @throws {RangeError} `options.additionalDigits` must be 0, 1 or 2
* @throws {RangeError} `options.locale` must contain `localize` property
* @throws {RangeError} `options.locale` must contain `formatLong` property
* @throws {RangeError} `options.locale` must contain `formatRelative` property
*/
export default function formatRelative (dirtyDate, dirtyBaseDate, dirtyOptions) {
if (arguments.length < 2) {
throw new TypeError('2 arguments required, but only ' + arguments.length + ' present')
}
var date = toDate(dirtyDate, dirtyOptions)
var baseDate = toDate(dirtyBaseDate, dirtyOptions)
var options = dirtyOptions || {}
var locale = options.locale || defaultLocale
if (!locale.localize) {
throw new RangeError('locale must contain localize property')
}
if (!locale.formatLong) {
throw new RangeError('locale must contain formatLong property')
}
if (!locale.formatRelative) {
throw new RangeError('locale must contain formatRelative property')
}
var diff = differenceInCalendarDays(date, baseDate, options)
if (isNaN(diff)) {
return 'Invalid Date'
}
var token
if (diff < -6) {
token = 'other'
} else if (diff < -1) {
token = 'lastWeek'
} else if (diff < 0) {
token = 'yesterday'
} else if (diff < 1) {
token = 'today'
} else if (diff < 2) {
token = 'tomorrow'
} else if (diff < 7) {
token = 'nextWeek'
} else {
token = 'other'
}
var utcDate = subMilliseconds(date, getTimezoneOffsetInMilliseconds(date), options)
var utcBaseDate = subMilliseconds(baseDate, getTimezoneOffsetInMilliseconds(baseDate), options)
var formatStr = locale.formatRelative(token, utcDate, utcBaseDate, options)
return format(date, formatStr, options)
}