/relative

Format the relative time between the current date and a given date in a human readable format.

Primary LanguageJavaScriptMIT LicenseMIT

relative (Relative Time Format)

npm npm GitHub

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.

Install

$ npm install @radulucut/relative --save

Usage

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"