Get Relative Time

A helpful utility to display relative time in short format like 3h, 2mo, 1y, 30s.

import { formatDistanceToNow } from "date-fns";

export function getRelativeTime(date: Date) {
  const distance = formatDistanceToNow(new Date(date), {
    includeSeconds: true,
  });

  return distance
    .replace("about ", "")
    .replace("less than ", "")
    .replace(" minutes", "m")
    .replace("half a minute", "30s")
    .replace("a minute", "1m")
    .replace(" minute", "m")
    .replace(" hours", "h")
    .replace(" hour", "h")
    .replace(" days", "d")
    .replace(" day", "d")
    .replace(" weeks", "w")
    .replace(" week", "w")
    .replace(" months", "mo")
    .replace(" month", "mo")
    .replace(" years", "y")
    .replace(" year", "y")
    .replace(" seconds", "s")
    .replace(" second", "s");
}

Loved this post?

Comments

Interested in
working
with me?