To use formatDistance
function from date-fns in JavaScript, first, we need to install the date-fns package in our project using npm or yarn.
21 chars2 lines
Once we installed the date-fns package in our project. We can import the formatDistance
function from the package in our JavaScript file.
43 chars2 lines
Now, we can use this formatDistance
function to get the distance between two dates in human-readable format. The function takes two arguments. The first argument is the starting date, and the second argument is the ending date to get the distance between them.
202 chars10 lines
We can also pass the third argument to the formatDistance
function as an object to provide options like includeSeconds
, addSuffix
, locale
, etc.
114 chars5 lines
That's how we can use formatDistance
function from date-fns package in JavaScript to get the distance between two dates in human-readable format.
gistlibby LogSnag