The formatDistanceToNow
function in date-fns is used to format the distance between a given date and the current date/time in a human-readable format, such as "3 days ago" or "in 5 hours".
To use this function, first install the date-fns
package via npm:
index.tsx21 chars2 lines
Then, import the function into your javascript file:
index.tsx48 chars2 lines
You can then call the function, passing in the date to format as a parameter:
index.tsx110 chars3 lines
You can also provide additional options to customize the format of the output, such as specifying the addSuffix
option to include "ago" or "in" in the output:
index.tsx155 chars4 lines
gistlibby LogSnag