useTimeAgo

Formats the distance between a timestamp and now — “همین الان”, “۵ دقیقه پیش”, “دیروز” — and keeps that string current by re-rendering on an interval. Works with Persian or English messages, and can fall back to an absolute date past a threshold.

Last updated August 12, 2026

Overview

Installation

$ npx shadcn@latest add https://ui.persian-labs.ir/r/use-time-ago.json

Usage

import { useTimeAgo } from "@/hooks/use-time-ago"
 
function LastSeen({ at }: { at: string }) {
  const ago = useTimeAgo(at)
 
  return <span>{ago}</span> // "۲ ساعت پیش"
}

Examples

English locale

Pass locale: "en" to switch to the built-in English message table.

Custom update interval

updateInterval controls how often the string re-renders — shorten it for a live-feeling "seconds ago" counter.

Falling back to a full date

max caps how far back the relative format goes; beyond it, fullDateFormatter renders an absolute date instead.

Showing seconds

By default, anything under a minute collapses to "just now". showSecond keeps the seconds unit instead.

Custom messages

Supply your own TimeAgoMessages table to fully control the wording for every unit.

RTL

The default Persian message table already reads correctly right-to-left; there's no bidi-specific fix needed here beyond using it inside Persian content, as shown below.

API Reference

useTimeAgo(time, options?)

formatTimeAgo

TimeAgoMessages

Built-in message tables