timeAgo

Returns a relative time string (e.g., `2 hours ago`) from a given date to the current time. This function is ideal for creating human-readable time differences, such as for activity feeds or notifications.
> timeAgo(date: Date | null | undefined): string
Parameters
  • date: The date to calculate the time elapsed from.
  • Returns: A human-readable string representing the time elapsed since the given date.

Example

import { timeAgo } from "@explita/daily-toolset";

const date = new Date();

console.log(timeAgo(date)); // "a few seconds ago"

date.setMinutes(date.getMinutes() - 1);
console.log(timeAgo(date)); // "1 minute ago"


date.setHours(date.getHours() - 1);
console.log(timeAgo(date)); // "1 hour ago"