-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatRelative_index.js.html
146 lines (118 loc) · 16.6 KB
/
formatRelative_index.js.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: formatRelative/index.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: formatRelative/index.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import differenceInCalendarDays from '../differenceInCalendarDays/index'
import format from '../format/index'
import defaultLocale from '../locale/en-US/index'
import subMilliseconds from '../subMilliseconds/index'
import toDate from '../toDate/index'
import getTimezoneOffsetInMilliseconds from '../_lib/getTimezoneOffsetInMilliseconds/index'
import requiredArgs from '../_lib/requiredArgs/index'
/**
* @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 |
*
* ### 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).
*
* @param {Date|Number} date - the date to format
* @param {Date|Number} baseDate - the date to compare with
* @param {Object} [options] - an object with options.
* @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
* @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
* @returns {String} the date 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.weekStartsOn` must be between 0 and 6
* @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) {
requiredArgs(2, arguments)
var date = toDate(dirtyDate)
var baseDate = toDate(dirtyBaseDate)
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)
if (isNaN(diff)) {
throw new RangeError('Invalid time value')
}
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))
var utcBaseDate = subMilliseconds(
baseDate,
getTimezoneOffsetInMilliseconds(baseDate)
)
var formatStr = locale.formatRelative(token, utcDate, utcBaseDate, options)
return format(date, formatStr, options)
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#add">add</a></li><li><a href="global.html#addBusinessDays">addBusinessDays</a></li><li><a href="global.html#addDays">addDays</a></li><li><a href="global.html#addHours">addHours</a></li><li><a href="global.html#addISOWeekYears">addISOWeekYears</a></li><li><a href="global.html#addMilliseconds">addMilliseconds</a></li><li><a href="global.html#addMinutes">addMinutes</a></li><li><a href="global.html#addMonths">addMonths</a></li><li><a href="global.html#addQuarters">addQuarters</a></li><li><a href="global.html#addSeconds">addSeconds</a></li><li><a href="global.html#addWeeks">addWeeks</a></li><li><a href="global.html#addYears">addYears</a></li><li><a href="global.html#areIntervalsOverlapping">areIntervalsOverlapping</a></li><li><a href="global.html#closestIndexTo">closestIndexTo</a></li><li><a href="global.html#closestTo">closestTo</a></li><li><a href="global.html#compareAsc">compareAsc</a></li><li><a href="global.html#compareDesc">compareDesc</a></li><li><a href="global.html#differenceInBusinessDays">differenceInBusinessDays</a></li><li><a href="global.html#differenceInCalendarDays">differenceInCalendarDays</a></li><li><a href="global.html#differenceInCalendarISOWeeks">differenceInCalendarISOWeeks</a></li><li><a href="global.html#differenceInCalendarISOWeekYears">differenceInCalendarISOWeekYears</a></li><li><a href="global.html#differenceInCalendarMonths">differenceInCalendarMonths</a></li><li><a href="global.html#differenceInCalendarQuarters">differenceInCalendarQuarters</a></li><li><a href="global.html#differenceInCalendarWeeks">differenceInCalendarWeeks</a></li><li><a href="global.html#differenceInCalendarYears">differenceInCalendarYears</a></li><li><a href="global.html#differenceInDays">differenceInDays</a></li><li><a href="global.html#differenceInHours">differenceInHours</a></li><li><a href="global.html#differenceInISOWeekYears">differenceInISOWeekYears</a></li><li><a href="global.html#differenceInMilliseconds">differenceInMilliseconds</a></li><li><a href="global.html#differenceInMinutes">differenceInMinutes</a></li><li><a href="global.html#differenceInMonths">differenceInMonths</a></li><li><a href="global.html#differenceInQuarters">differenceInQuarters</a></li><li><a href="global.html#differenceInSeconds">differenceInSeconds</a></li><li><a href="global.html#differenceInWeeks">differenceInWeeks</a></li><li><a href="global.html#differenceInYears">differenceInYears</a></li><li><a href="global.html#eachDayOfInterval">eachDayOfInterval</a></li><li><a href="global.html#eachHourOfInterval">eachHourOfInterval</a></li><li><a href="global.html#eachMonthOfInterval">eachMonthOfInterval</a></li><li><a href="global.html#eachQuarterOfInterval">eachQuarterOfInterval</a></li><li><a href="global.html#eachWeekendOfInterval">eachWeekendOfInterval</a></li><li><a href="global.html#eachWeekendOfMonth">eachWeekendOfMonth</a></li><li><a href="global.html#eachWeekendOfYear">eachWeekendOfYear</a></li><li><a href="global.html#eachWeekOfInterval">eachWeekOfInterval</a></li><li><a href="global.html#eachYearOfInterval">eachYearOfInterval</a></li><li><a href="global.html#endOfDay">endOfDay</a></li><li><a href="global.html#endOfDecade">endOfDecade</a></li><li><a href="global.html#endOfHour">endOfHour</a></li><li><a href="global.html#endOfISOWeek">endOfISOWeek</a></li><li><a href="global.html#endOfISOWeekYear">endOfISOWeekYear</a></li><li><a href="global.html#endOfMinute">endOfMinute</a></li><li><a href="global.html#endOfMonth">endOfMonth</a></li><li><a href="global.html#endOfQuarter">endOfQuarter</a></li><li><a href="global.html#endOfSecond">endOfSecond</a></li><li><a href="global.html#endOfToday">endOfToday</a></li><li><a href="global.html#endOfTomorrow">endOfTomorrow</a></li><li><a href="global.html#endOfWeek">endOfWeek</a></li><li><a href="global.html#endOfYear">endOfYear</a></li><li><a href="global.html#endOfYesterday">endOfYesterday</a></li><li><a href="global.html#eraValues">eraValues</a></li><li><a href="global.html#format">format</a></li><li><a href="global.html#formatDistance">formatDistance</a></li><li><a href="global.html#formatDistanceLocale">formatDistanceLocale</a></li><li><a href="global.html#formatDistanceStrict">formatDistanceStrict</a></li><li><a href="global.html#formatDistanceToNow">formatDistanceToNow</a></li><li><a href="global.html#formatDistanceToNowStrict">formatDistanceToNowStrict</a></li><li><a href="global.html#formatDuration">formatDuration</a></li><li><a href="global.html#formatISO">formatISO</a></li><li><a href="global.html#formatISO9075">formatISO9075</a></li><li><a href="global.html#formatISODuration">formatISODuration</a></li><li><a href="global.html#formatRelative">formatRelative</a></li><li><a href="global.html#formatRFC3339">formatRFC3339</a></li><li><a href="global.html#formatRFC7231">formatRFC7231</a></li><li><a href="global.html#fromUnixTime">fromUnixTime</a></li><li><a href="global.html#getDate">getDate</a></li><li><a href="global.html#getDay">getDay</a></li><li><a href="global.html#getDayOfYear">getDayOfYear</a></li><li><a href="global.html#getDaysInMonth">getDaysInMonth</a></li><li><a href="global.html#getDaysInYear">getDaysInYear</a></li><li><a href="global.html#getDecade">getDecade</a></li><li><a href="global.html#getHours">getHours</a></li><li><a href="global.html#getISODay">getISODay</a></li><li><a href="global.html#getISOWeek">getISOWeek</a></li><li><a href="global.html#getISOWeeksInYear">getISOWeeksInYear</a></li><li><a href="global.html#getISOWeekYear">getISOWeekYear</a></li><li><a href="global.html#getMilliseconds">getMilliseconds</a></li><li><a href="global.html#getMinutes">getMinutes</a></li><li><a href="global.html#getMonth">getMonth</a></li><li><a href="global.html#getOverlappingDaysInIntervals">getOverlappingDaysInIntervals</a></li><li><a href="global.html#getQuarter">getQuarter</a></li><li><a href="global.html#getSeconds">getSeconds</a></li><li><a href="global.html#getTime">getTime</a></li><li><a href="global.html#getTimezoneOffsetInMilliseconds">getTimezoneOffsetInMilliseconds</a></li><li><a href="global.html#getUnixTime">getUnixTime</a></li><li><a href="global.html#getWeek">getWeek</a></li><li><a href="global.html#getWeekOfMonth">getWeekOfMonth</a></li><li><a href="global.html#getWeeksInMonth">getWeeksInMonth</a></li><li><a href="global.html#getWeekYear">getWeekYear</a></li><li><a href="global.html#getYear">getYear</a></li><li><a href="global.html#intervalToDuration">intervalToDuration</a></li><li><a href="global.html#isAfter">isAfter</a></li><li><a href="global.html#isBefore">isBefore</a></li><li><a href="global.html#isDate">isDate</a></li><li><a href="global.html#isEqual">isEqual</a></li><li><a href="global.html#isExists">isExists</a></li><li><a href="global.html#isFirstDayOfMonth">isFirstDayOfMonth</a></li><li><a href="global.html#isFriday">isFriday</a></li><li><a href="global.html#isFuture">isFuture</a></li><li><a href="global.html#isLastDayOfMonth">isLastDayOfMonth</a></li><li><a href="global.html#isLeapYear">isLeapYear</a></li><li><a href="global.html#isMatch">isMatch</a></li><li><a href="global.html#isMonday">isMonday</a></li><li><a href="global.html#isPast">isPast</a></li><li><a href="global.html#isSameDay">isSameDay</a></li><li><a href="global.html#isSameHour">isSameHour</a></li><li><a href="global.html#isSameISOWeek">isSameISOWeek</a></li><li><a href="global.html#isSameISOWeekYear">isSameISOWeekYear</a></li><li><a href="global.html#isSameMinute">isSameMinute</a></li><li><a href="global.html#isSameMonth">isSameMonth</a></li><li><a href="global.html#isSameQuarter">isSameQuarter</a></li><li><a href="global.html#isSameSecond">isSameSecond</a></li><li><a href="global.html#isSameWeek">isSameWeek</a></li><li><a href="global.html#isSameYear">isSameYear</a></li><li><a href="global.html#isSaturday">isSaturday</a></li><li><a href="global.html#isSunday">isSunday</a></li><li><a href="global.html#isThisHour">isThisHour</a></li><li><a href="global.html#isThisISOWeek">isThisISOWeek</a></li><li><a href="global.html#isThisMinute">isThisMinute</a></li><li><a href="global.html#isThisMonth">isThisMonth</a></li><li><a href="global.html#isThisQuarter">isThisQuarter</a></li><li><a href="global.html#isThisSecond">isThisSecond</a></li><li><a href="global.html#isThisWeek">isThisWeek</a></li><li><a href="global.html#isThisYear">isThisYear</a></li><li><a href="global.html#isThursday">isThursday</a></li><li><a href="global.html#isToday">isToday</a></li><li><a href="global.html#isTomorrow">isTomorrow</a></li><li><a href="global.html#isTuesday">isTuesday</a></li><li><a href="global.html#isValid">isValid</a></li><li><a href="global.html#isWednesday">isWednesday</a></li><li><a href="global.html#isWeekend">isWeekend</a></li><li><a href="global.html#isWithinInterval">isWithinInterval</a></li><li><a href="global.html#isYesterday">isYesterday</a></li><li><a href="global.html#lastDayOfDecade">lastDayOfDecade</a></li><li><a href="global.html#lastDayOfISOWeek">lastDayOfISOWeek</a></li><li><a href="global.html#lastDayOfISOWeekYear">lastDayOfISOWeekYear</a></li><li><a href="global.html#lastDayOfMonth">lastDayOfMonth</a></li><li><a href="global.html#lastDayOfQuarter">lastDayOfQuarter</a></li><li><a href="global.html#lastDayOfWeek">lastDayOfWeek</a></li><li><a href="global.html#lastDayOfYear">lastDayOfYear</a></li><li><a href="global.html#lightFormat">lightFormat</a></li><li><a href="global.html#locale">locale</a></li><li><a href="global.html#max">max</a></li><li><a href="global.html#maxTime">maxTime</a></li><li><a href="global.html#milliseconds">milliseconds</a></li><li><a href="global.html#min">min</a></li><li><a href="global.html#parse">parse</a></li><li><a href="global.html#parseISO">parseISO</a></li><li><a href="global.html#parseJSON">parseJSON</a></li><li><a href="global.html#roundToNearestMinutes">roundToNearestMinutes</a></li><li><a href="global.html#set">set</a></li><li><a href="global.html#setDate">setDate</a></li><li><a href="global.html#setDay">setDay</a></li><li><a href="global.html#setDayOfYear">setDayOfYear</a></li><li><a href="global.html#setHours">setHours</a></li><li><a href="global.html#setISODay">setISODay</a></li><li><a href="global.html#setISOWeek">setISOWeek</a></li><li><a href="global.html#setISOWeekYear">setISOWeekYear</a></li><li><a href="global.html#setMilliseconds">setMilliseconds</a></li><li><a href="global.html#setMinutes">setMinutes</a></li><li><a href="global.html#setMonth">setMonth</a></li><li><a href="global.html#setQuarter">setQuarter</a></li><li><a href="global.html#setSeconds">setSeconds</a></li><li><a href="global.html#setWeek">setWeek</a></li><li><a href="global.html#setWeekYear">setWeekYear</a></li><li><a href="global.html#setYear">setYear</a></li><li><a href="global.html#startOfDay">startOfDay</a></li><li><a href="global.html#startOfDecade">startOfDecade</a></li><li><a href="global.html#startOfHour">startOfHour</a></li><li><a href="global.html#startOfISOWeek">startOfISOWeek</a></li><li><a href="global.html#startOfISOWeekYear">startOfISOWeekYear</a></li><li><a href="global.html#startOfMinute">startOfMinute</a></li><li><a href="global.html#startOfMonth">startOfMonth</a></li><li><a href="global.html#startOfQuarter">startOfQuarter</a></li><li><a href="global.html#startOfSecond">startOfSecond</a></li><li><a href="global.html#startOfToday">startOfToday</a></li><li><a href="global.html#startOfTomorrow">startOfTomorrow</a></li><li><a href="global.html#startOfWeek">startOfWeek</a></li><li><a href="global.html#startOfWeekYear">startOfWeekYear</a></li><li><a href="global.html#startOfYear">startOfYear</a></li><li><a href="global.html#startOfYesterday">startOfYesterday</a></li><li><a href="global.html#sub">sub</a></li><li><a href="global.html#subBusinessDays">subBusinessDays</a></li><li><a href="global.html#subDays">subDays</a></li><li><a href="global.html#subHours">subHours</a></li><li><a href="global.html#subISOWeekYears">subISOWeekYears</a></li><li><a href="global.html#subMilliseconds">subMilliseconds</a></li><li><a href="global.html#subMinutes">subMinutes</a></li><li><a href="global.html#subMonths">subMonths</a></li><li><a href="global.html#subQuarters">subQuarters</a></li><li><a href="global.html#subSeconds">subSeconds</a></li><li><a href="global.html#subWeeks">subWeeks</a></li><li><a href="global.html#subYears">subYears</a></li><li><a href="global.html#toDate">toDate</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Fri Apr 16 2021 13:00:04 GMT+0800 (GMT+08:00)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>