Get Time Ago

Returns a human-readable relative time string based on a given timestamp.

CategoryDates
AuthorPol Gubau
Since1.0.0
Last Updated2025-03-15
TypeSync
CompatibilityUniversal

Example

  • Input

    getTimeAgo(1616161600000)

    Output:

    2 days ago
  • Input

    getTimeAgo(1616161600000, { locale: 'es' })

    Output:

    hace 2 días

Return

A human-readable string indicating the relative time (e.g., '2 days ago').

Type: string

Props

NameTypeDefaultRequiredDescription
timestampnumbernullYesThe timestamp to compare the current time against, in milliseconds.
optionsGetTimeAgoOptions{ locale: undefined }NoOptional settings, including the locale for formatting the relative time.

Notes

This function computes the difference between the current time and the provided timestamp, and returns a formatted string such as 'X days ago', 'X hours ago', or 'X minutes ago', based on the elapsed time.

Tags

  • time
  • date
  • relativeTime
  • intl
  • locale