To use the formatRelative
function from the date-fns
library in TypeScript, you can follow these steps:
date-fns
library and its associated types. You can do this by running the following command in your terminal:index.ts37 chars2 lines
formatRelative
function from the date-fns
module in your TypeScript code as follows:index.ts43 chars2 lines
formatRelative
function takes two arguments: a date
argument (which can be a Date
object or a date string in ISO format) and an optional options
argument (which is an object of type LocaleOptions
). You can use the function to format the input date as a relative time string (e.g. "5 days ago" or "in 3 months"). Here's an example usage:index.ts249 chars4 lines
You can customize the options
argument to use a different locale, provide a baseDate
to calculate the relative time from, specify the unit
of the time string (e.g. "week" or "day"), and more. Please refer to the date-fns
documentation for more information on the available options.
gistlibby LogSnag