123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import differenceInCalendarDays from "../differenceInCalendarDays/index.js";
- import format from "../format/index.js";
- import defaultLocale from "../locale/en-US/index.js";
- import subMilliseconds from "../subMilliseconds/index.js";
- import toDate from "../toDate/index.js";
- import getTimezoneOffsetInMilliseconds from "../_lib/getTimezoneOffsetInMilliseconds/index.js";
- import requiredArgs from "../_lib/requiredArgs/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 |
- *
- * ### 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 _ref = dirtyOptions || {},
- _ref$locale = _ref.locale,
- locale = _ref$locale === void 0 ? defaultLocale : _ref$locale,
- _ref$weekStartsOn = _ref.weekStartsOn,
- weekStartsOn = _ref$weekStartsOn === void 0 ? 0 : _ref$weekStartsOn;
- 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, {
- locale: locale,
- weekStartsOn: weekStartsOn
- });
- return format(date, formatStr, {
- locale: locale,
- weekStartsOn: weekStartsOn
- });
- }
|