2026-07-05 06:41:32 +00:00
"use strict" ;
Object . defineProperty ( exports , "__esModule" , {
value : true
} ) ;
exports . default = formatDistanceStrict ;
2026-07-07 07:11:50 +00:00
var _index = _interopRequireDefault ( require ( "../_lib/getTimezoneOffsetInMilliseconds/index.js" ) ) ;
var _index2 = _interopRequireDefault ( require ( "../compareAsc/index.js" ) ) ;
var _index3 = _interopRequireDefault ( require ( "../toDate/index.js" ) ) ;
var _index4 = _interopRequireDefault ( require ( "../_lib/cloneObject/index.js" ) ) ;
var _index5 = _interopRequireDefault ( require ( "../locale/en-US/index.js" ) ) ;
var _index6 = _interopRequireDefault ( require ( "../_lib/requiredArgs/index.js" ) ) ;
function _interopRequireDefault ( obj ) { return obj && obj . _ _esModule ? obj : { default : obj } ; }
2026-07-05 06:41:32 +00:00
var MILLISECONDS _IN _MINUTE = 1000 * 60 ;
var MINUTES _IN _DAY = 60 * 24 ;
var MINUTES _IN _MONTH = MINUTES _IN _DAY * 30 ;
var MINUTES _IN _YEAR = MINUTES _IN _DAY * 365 ;
/**
* @name formatDistanceStrict
* @category Common Helpers
* @summary Return the distance between the given dates in words.
*
* @description
* Return the distance between the given dates in words, using strict units.
* This is like `formatDistance`, but does not use helpers like 'almost', 'over',
* 'less than' and the like.
*
* | Distance between dates | Result |
* |------------------------|---------------------|
* | 0 ... 59 secs | [0..59] seconds |
* | 1 ... 59 mins | [1..59] minutes |
* | 1 ... 23 hrs | [1..23] hours |
* | 1 ... 29 days | [1..29] days |
* | 1 ... 11 months | [1..11] months |
* | 1 ... N years | [1..N] years |
*
2026-07-07 07:11:50 +00:00
* ### 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 `distanceInWordsStrict` to `formatDistanceStrict`
* 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
*
* distanceInWordsStrict(
* new Date(2015, 0, 2),
* new Date(2014, 6, 2)
* ) //=> '6 months'
*
* // v2.0.0 onward
*
* formatDistanceStrict(
* new Date(2014, 6, 2),
* new Date(2015, 0, 2)
* ) //=> '6 months'
* ```
*
* - `partialMethod` option is renamed to `roundingMethod`.
*
* ```javascript
* // Before v2.0.0
*
* distanceInWordsStrict(
* new Date(1986, 3, 4, 10, 32, 0),
* new Date(1986, 3, 4, 10, 33, 1),
* { partialMethod: 'ceil' }
* ) //=> '2 minutes'
*
* // v2.0.0 onward
*
* formatDistanceStrict(
* new Date(1986, 3, 4, 10, 33, 1),
* new Date(1986, 3, 4, 10, 32, 0),
* { roundingMethod: 'ceil' }
* ) //=> '2 minutes'
* ```
*
* - If `roundingMethod` is not specified, it now defaults to `round` instead of `floor`.
*
* - `unit` option now accepts one of the strings:
* 'second', 'minute', 'hour', 'day', 'month' or 'year' instead of 's', 'm', 'h', 'd', 'M' or 'Y'
*
* ```javascript
* // Before v2.0.0
*
* distanceInWordsStrict(
* new Date(1986, 3, 4, 10, 32, 0),
* new Date(1986, 3, 4, 10, 33, 1),
* { unit: 'm' }
* )
*
* // v2.0.0 onward
*
* formatDistanceStrict(
* new Date(1986, 3, 4, 10, 33, 1),
* new Date(1986, 3, 4, 10, 32, 0),
* { unit: 'minute' }
* )
* ```
*
2026-07-05 06:41:32 +00:00
* @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.addSuffix=false] - result indicates if the second date is earlier or later than the first
* @param {'second'|'minute'|'hour'|'day'|'month'|'year'} [options.unit] - if specified, will force a unit
* @param {'floor'|'ceil'|'round'} [options.roundingMethod='round'] - which way to round partial units
* @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.roundingMethod` must be 'floor', 'ceil' or 'round'
* @throws {RangeError} `options.unit` must be 'second', 'minute', 'hour', 'day', 'month' or 'year'
* @throws {RangeError} `options.locale` must contain `formatDistance` property
*
* @example
* // What is the distance between 2 July 2014 and 1 January 2015?
2026-07-07 07:11:50 +00:00
* var result = formatDistanceStrict(new Date(2014, 6, 2), new Date(2015, 0, 2))
2026-07-05 06:41:32 +00:00
* //=> '6 months'
*
* @example
* // What is the distance between 1 January 2015 00:00:15
* // and 1 January 2015 00:00:00?
2026-07-07 07:11:50 +00:00
* var result = formatDistanceStrict(
2026-07-05 06:41:32 +00:00
* new Date(2015, 0, 1, 0, 0, 15),
* new Date(2015, 0, 1, 0, 0, 0)
* )
* //=> '15 seconds'
*
* @example
* // What is the distance from 1 January 2016
* // to 1 January 2015, with a suffix?
2026-07-07 07:11:50 +00:00
* var result = formatDistanceStrict(new Date(2015, 0, 1), new Date(2016, 0, 1), {
2026-07-05 06:41:32 +00:00
* addSuffix: true
* })
* //=> '1 year ago'
*
* @example
* // What is the distance from 1 January 2016
* // to 1 January 2015, in minutes?
2026-07-07 07:11:50 +00:00
* var result = formatDistanceStrict(new Date(2016, 0, 1), new Date(2015, 0, 1), {
2026-07-05 06:41:32 +00:00
* unit: 'minute'
* })
* //=> '525600 minutes'
*
* @example
* // What is the distance from 1 January 2015
* // to 28 January 2015, in months, rounded up?
2026-07-07 07:11:50 +00:00
* var result = formatDistanceStrict(new Date(2015, 0, 28), new Date(2015, 0, 1), {
2026-07-05 06:41:32 +00:00
* unit: 'month',
* roundingMethod: 'ceil'
* })
* //=> '1 month'
*
* @example
* // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto?
* import { eoLocale } from 'date-fns/locale/eo'
2026-07-07 07:11:50 +00:00
* var result = formatDistanceStrict(new Date(2016, 7, 1), new Date(2015, 0, 1), {
2026-07-05 06:41:32 +00:00
* locale: eoLocale
* })
* //=> '1 jaro'
*/
2026-07-07 07:11:50 +00:00
function formatDistanceStrict ( dirtyDate , dirtyBaseDate , dirtyOptions ) {
( 0 , _index6 . default ) ( 2 , arguments ) ;
var options = dirtyOptions || { } ;
var locale = options . locale || _index5 . default ;
2026-07-05 06:41:32 +00:00
if ( ! locale . formatDistance ) {
throw new RangeError ( 'locale must contain localize.formatDistance property' ) ;
}
2026-07-07 07:11:50 +00:00
var comparison = ( 0 , _index2 . default ) ( dirtyDate , dirtyBaseDate ) ;
2026-07-05 06:41:32 +00:00
if ( isNaN ( comparison ) ) {
throw new RangeError ( 'Invalid time value' ) ;
}
2026-07-07 07:11:50 +00:00
var localizeOptions = ( 0 , _index4 . default ) ( options ) ;
localizeOptions . addSuffix = Boolean ( options . addSuffix ) ;
localizeOptions . comparison = comparison ;
2026-07-05 06:41:32 +00:00
var dateLeft ;
var dateRight ;
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
if ( comparison > 0 ) {
2026-07-07 07:11:50 +00:00
dateLeft = ( 0 , _index3 . default ) ( dirtyBaseDate ) ;
dateRight = ( 0 , _index3 . default ) ( dirtyDate ) ;
2026-07-05 06:41:32 +00:00
} else {
2026-07-07 07:11:50 +00:00
dateLeft = ( 0 , _index3 . default ) ( dirtyDate ) ;
dateRight = ( 0 , _index3 . default ) ( dirtyBaseDate ) ;
2026-07-05 06:41:32 +00:00
}
2026-07-07 07:11:50 +00:00
var roundingMethod = options . roundingMethod == null ? 'round' : String ( options . roundingMethod ) ;
2026-07-05 06:41:32 +00:00
var roundingMethodFn ;
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
if ( roundingMethod === 'floor' ) {
roundingMethodFn = Math . floor ;
} else if ( roundingMethod === 'ceil' ) {
roundingMethodFn = Math . ceil ;
} else if ( roundingMethod === 'round' ) {
roundingMethodFn = Math . round ;
} else {
throw new RangeError ( "roundingMethod must be 'floor', 'ceil' or 'round'" ) ;
}
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
var milliseconds = dateRight . getTime ( ) - dateLeft . getTime ( ) ;
var minutes = milliseconds / MILLISECONDS _IN _MINUTE ;
2026-07-07 07:11:50 +00:00
var timezoneOffset = ( 0 , _index . default ) ( dateRight ) - ( 0 , _index . default ) ( dateLeft ) ; // Use DST-normalized difference in minutes for years, months and days;
2026-07-05 06:41:32 +00:00
// use regular difference in minutes for hours, minutes and seconds.
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
var dstNormalizedMinutes = ( milliseconds - timezoneOffset ) / MILLISECONDS _IN _MINUTE ;
var unit ;
2026-07-07 07:11:50 +00:00
if ( options . unit == null ) {
2026-07-05 06:41:32 +00:00
if ( minutes < 1 ) {
unit = 'second' ;
} else if ( minutes < 60 ) {
unit = 'minute' ;
} else if ( minutes < MINUTES _IN _DAY ) {
unit = 'hour' ;
} else if ( dstNormalizedMinutes < MINUTES _IN _MONTH ) {
unit = 'day' ;
} else if ( dstNormalizedMinutes < MINUTES _IN _YEAR ) {
unit = 'month' ;
} else {
unit = 'year' ;
}
} else {
2026-07-07 07:11:50 +00:00
unit = String ( options . unit ) ;
} // 0 up to 60 seconds
2026-07-05 06:41:32 +00:00
if ( unit === 'second' ) {
var seconds = roundingMethodFn ( milliseconds / 1000 ) ;
2026-07-07 07:11:50 +00:00
return locale . formatDistance ( 'xSeconds' , seconds , localizeOptions ) ; // 1 up to 60 mins
2026-07-05 06:41:32 +00:00
} else if ( unit === 'minute' ) {
var roundedMinutes = roundingMethodFn ( minutes ) ;
2026-07-07 07:11:50 +00:00
return locale . formatDistance ( 'xMinutes' , roundedMinutes , localizeOptions ) ; // 1 up to 24 hours
2026-07-05 06:41:32 +00:00
} else if ( unit === 'hour' ) {
var hours = roundingMethodFn ( minutes / 60 ) ;
2026-07-07 07:11:50 +00:00
return locale . formatDistance ( 'xHours' , hours , localizeOptions ) ; // 1 up to 30 days
2026-07-05 06:41:32 +00:00
} else if ( unit === 'day' ) {
var days = roundingMethodFn ( dstNormalizedMinutes / MINUTES _IN _DAY ) ;
2026-07-07 07:11:50 +00:00
return locale . formatDistance ( 'xDays' , days , localizeOptions ) ; // 1 up to 12 months
2026-07-05 06:41:32 +00:00
} else if ( unit === 'month' ) {
var months = roundingMethodFn ( dstNormalizedMinutes / MINUTES _IN _MONTH ) ;
2026-07-07 07:11:50 +00:00
return months === 12 && options . unit !== 'month' ? locale . formatDistance ( 'xYears' , 1 , localizeOptions ) : locale . formatDistance ( 'xMonths' , months , localizeOptions ) ; // 1 year up to max Date
2026-07-05 06:41:32 +00:00
} else if ( unit === 'year' ) {
var years = roundingMethodFn ( dstNormalizedMinutes / MINUTES _IN _YEAR ) ;
return locale . formatDistance ( 'xYears' , years , localizeOptions ) ;
}
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
throw new RangeError ( "unit must be 'second', 'minute', 'hour', 'day', 'month' or 'year'" ) ;
}
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
module . exports = exports . default ;