On this page
formatDistanceToNowStrict
Return the distance between the given date and now 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 |
Usage
// CommonJS
var formatDistanceToNowStrict = require('date-fns/formatDistanceToNowStrict')
// ES 2015
import formatDistanceToNowStrict from 'date-fns/formatDistanceToNowStrict'
// ESM
import { formatDistanceToNowStrict } from 'date-fns'
Syntax
formatDistanceToNowStrict(date, [options])
Arguments
Name | Description |
---|---|
date |
the given date |
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} |
Returns
Description |
---|
the distance in words |
Exceptions
Type | Description |
---|---|
TypeError |
1 argument required |
RangeError |
|
RangeError |
|
Examples
// If today is 1 January 2015, what is the distance to 2 July 2014?
const result = formatDistanceToNowStrict(
new Date(2014, 6, 2)
)
//=> '6 months'
// If now is 1 January 2015 00:00:00,
// what is the distance to 1 January 2015 00:00:15, including seconds?
const result = formatDistanceToNowStrict(
new Date(2015, 0, 1, 0, 0, 15)
)
//=> '15 seconds'
// If today is 1 January 2015,
// what is the distance to 1 January 2016, with a suffix?
const result = formatDistanceToNowStrict(
new Date(2016, 0, 1),
{addSuffix: true}
)
//=> 'in 1 year'
// If today is 28 January 2015,
// what is the distance to 1 January 2015, in months, rounded up??
const result = formatDistanceToNowStrict(new Date(2015, 0, 1), {
unit: 'month',
roundingMethod: 'ceil'
})
//=> '1 month'
// If today is 1 January 2015,
// what is the distance to 1 January 2016 in Esperanto?
const eoLocale = require('date-fns/locale/eo')
const result = formatDistanceToNowStrict(
new Date(2016, 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/formatDistanceToNowStrict