@ohfc/time-ago
v0.0.1
Published
<description>
Readme
@ohfc/time-ago
A minimal, dependency-free utility to format dates and timestamps as "time ago" or "in X" strings, supporting both Node.js and browser environments.
Installation
npm install @ohfc/time-agoFeatures
- Converts dates, timestamps, or ISO strings to readable relative time (e.g.,
"2 days ago","in 5 minutes") - Supports both Node.js and browser
- Localizable using native
Intl.RelativeTimeFormat - No dependencies
- Tiny footprint
Usage
import timeAgo from "@ohfc/time-ago";
// 1 minute ago, assuming Date.now()
console.log(timeAgo(Date.now() - 60 * 1000)); // "1 minute ago"
// In 2 hours
console.log(timeAgo(Date.now() + 2 * 60 * 60 * 1000)); // "in 2 hours"
// Pass a Date or ISO string
console.log(timeAgo(new Date(Date.now() - 86400000))); // "yesterday"
console.log(timeAgo("2025-09-06T12:00:00Z")); // relative to now
// Use a different locale
console.log(timeAgo(Date.now() - 3600 * 1000, "fr")); // "il y a 1 heure"API
timeAgo(input, locales?)
| Parameter | Type | Description |
| --------- | --------------------------------- | ------------------------------------------------------- |
| input | number \| string \| Date | The date value: timestamp, ISO string, or Date object |
| locales | Intl.LocalesArgument (optional) | Optional BCP-47 locale string (defaults to "en") |
Returns:
A string with the relative time, such as "5 minutes ago" or "tomorrow".
Throws:TypeError if input is not provided or invalid.
This README enables quick installation, highlights the main features, shows how to use all API options, and documents the accepted input types and method signature—all essential for an npm package introduction.[2][4]
