Reusables

Time Ago

A TypeScript utility for converting timestamps into human-readable relative time strings

Installation

npx shadcn@latest add "https://reusables.vercel.app/r/time-ago"

Usage

Basic usage with default options:

const time = timeAgo(date)
// "2 hours ago"

Enhanced formatting with options:

const time = formatTimeAgo(date, {
  short: true,
  maxUnits: 2,
})
// "2h 30m ago"

Example

Simple Time Ago
just now
3/25/2025, 6:06:24 PM
Enhanced Time Ago
just now
3/25/2025, 6:06:24 PM

API Reference

timeAgo(date)

PropTypeDefault
date
Date | string | number
-

formatTimeAgo(date, options)

PropTypeDefault
short
boolean
false
maxUnits
number
1
future
boolean
true
round
boolean
true
Edit on GitHub

Last updated on

On this page