getRelativeTimeString (util/dateUtil.ts)

slide.code · widgets/app/src/lib/util/dateUtil.ts · typescript · MIT
ui, tag:knn-classified

Utility that returns human‑readable relative time strings like '2 hours ago'.

// Source: https://github.com/longtail-labs/slide.code | License: MIT | Copyright 2025 longtail.labs
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Returns a string representing the relative time between the current time and the provided date
 * e.g. "2 hours ago", "5 days ago", "just now"
 */
export const getRelativeTimeString = (date: Date): string => {
  const now = new Date()
  const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000)

  if (diffInSeconds < 5) {
    return 'just now'
  }

  if (diffInSeconds < 60) {
    return `${diffInSeconds} secs ago`
  }

  const diffInMinutes = Math.floor(diffInSeconds / 60)
  if (diffInMinutes < 60) {
    return `${diffInMinutes} ${diffInMinutes === 1 ? 'min' : 'mins'} ago`
  }

  const diffInHours = Math.

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key