A minimal relative time string function
Check the Documentation in JSR
The timeAgo
function returns a human-readable string representing the time
elapsed since a given date. It supports producing strings like "just now", "5
minutes ago", and "a year ago" for quick and simple time formatting.
Supported intervals are "year", "month", "week", "day", "hour",
"minute", and "second".
The timeRemaining
function returns a human-readable string representing the
time remaining until a given date. It supports producing strings like "in 5
seconds", "in 5 minutes", and "in a year" for quick and simple time
formatting. Supported intervals are "year", "month", "week", "day",
"hour", "minute", and "second".
import { timeAgo } from "@egamagz/time-ago";
console.log(timeAgo(new Date())); // "just now"
console.log(timeAgo(new Date(Date.now() - 1000))); // "a second ago"
console.log(timeAgo(new Date(Date.now() - 1000 * 60 * 5))); // "5 minutes ago"
console.log(timeAgo(new Date(Date.now() - 1000 * 60 * 60 * 24 * 5))); // "5 days ago"
import { timeRemaining } from "@egamagz/time-ago";
console.log(timeRemaining(Date.now() + 5000)); // "in 5 seconds"
console.log(timeRemaining(Date.now() + 5 * 60 * 1000)); // "in 5 minutes"
console.log(timeRemaining(Date.now() + 3 * 60 * 60 * 1000)); // "in 3 hours"
console.log(timeRemaining(Date.now() + 60 * 60 * 1000)); // "in an hour"
// Weeks
console.log(timeRemaining(Date.now() + 3 * 7 * 24 * 60 * 60 * 1000)); // "in 3 weeks"
// Month (exact month difference)
console.log(timeRemaining(Date.now() + 30 * 24 * 60 * 60 * 1000)); // "in a month"
// Four weeks vs month example
console.log(timeRemaining(Date.now() + 4 * 7 * 24 * 60 * 60 * 1000)); // "in 4 weeks"
With Deno
deno add jsr:@egamagz/time-ago
With Bun
bunx jsr add @egamagz/time-ago
With NPM
npx jsr add @egamagz/time-ago
MIT License