Return the distance between the given dates in words.
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 |
// CommonJS var formatDistanceStrict = require('date-fns/formatDistanceStrict')
// ES 2015 import formatDistanceStrict from 'date-fns/formatDistanceStrict'
// ESM import { formatDistanceStrict } from 'date-fns'
formatDistanceStrict(date, baseDate, [options])
Name | Description |
---|---|
date |
the date |
baseDate |
the date to compare with |
options |
an object with options. |
options.addSuffix |
result indicates if the second date is earlier or later than the first |
options.unit |
if specified, will force a unit |
options.roundingMethod |
which way to round partial units |
options.locale |
the locale object. See [Locale]{@link https://date-fns.org/docs/Locale} |
Description |
---|
the distance in words |
Type | Description |
---|---|
TypeError |
2 arguments required |
RangeError |
|
RangeError |
|
RangeError |
|
RangeError |
|
RangeError |
|
// What is the distance between 2 July 2014 and 1 January 2015? const result = formatDistanceStrict(new Date(2014, 6, 2), new Date(2015, 0, 2)) //=> '6 months'
// What is the distance between 1 January 2015 00:00:15 // and 1 January 2015 00:00:00? const result = formatDistanceStrict( new Date(2015, 0, 1, 0, 0, 15), new Date(2015, 0, 1, 0, 0, 0) ) //=> '15 seconds'
// What is the distance from 1 January 2016 // to 1 January 2015, with a suffix? const result = formatDistanceStrict(new Date(2015, 0, 1), new Date(2016, 0, 1), { addSuffix: true }) //=> '1 year ago'
// What is the distance from 1 January 2016 // to 1 January 2015, in minutes? const result = formatDistanceStrict(new Date(2016, 0, 1), new Date(2015, 0, 1), { unit: 'minute' }) //=> '525600 minutes'
// What is the distance from 1 January 2015 // to 28 January 2015, in months, rounded up? const result = formatDistanceStrict(new Date(2015, 0, 28), new Date(2015, 0, 1), { unit: 'month', roundingMethod: 'ceil' }) //=> '1 month'
// What is the distance between 1 August 2016 and 1 January 2015 in Esperanto? import { eoLocale } from 'date-fns/locale/eo' const result = formatDistanceStrict(new Date(2016, 7, 1), new Date(2015, 0, 1), { locale: eoLocale }) //=> '1 jaro'
© 2021 Sasha Koss and Lesha Koss
Licensed under the MIT License.
https://date-fns.org/v2.29.2/docs/formatDistanceStrict