Format the relative time between the current date and a given date in a human readable format.
The output is rounded to the nearest time unit, i.e. 1 year ago
, 5 minutes ago
, in 3 days
, in 6 seconds
.
Built on top of the Intl.RelativeTimeFormat API. Supports multiple languages and custom formatting options.
$ npm install @radulucut/relative --save
import Time from "@radulucut/relative";
const time = Time();
time.Relative(new Date("2024-01-01T00:00:00.000Z")); // "2 months ago"
const timeRO = Time("ro", { numeric: "auto" });
timeRO.Relative(new Date("2024-05-01T00:00:00.000Z")); // "peste 2 luni"
const timeES = Time("es", { style: "long" });
timeES.Relative(new Date("2024-01-01T00:00:00.000Z")); // "hace 2 meses"