Skip to content

Time Ago (useTimeAgo)

useTimeAgo is a Vue composable function used to display "how long ago", while also providing its non-reactive method formatTimeAgo.

It can handle common time difference formats such as: seconds ago, minutes ago, hours ago, days ago, weeks ago, months ago, and years ago.

API Source Code: GitHub

Usage

vue
<template>
  <p>Published at: {{ ago }}</p>
</template>

<script setup>
import { ref } from "vue";
import { useTimeAgo } from "see-u-ui";

const publishedAt = ref("2025-01-01 12:00:00");

// Refreshes automatically every 30 seconds
const ago = useTimeAgo(publishedAt);
</script>
ts
import { formatTimeAgo } from "see-u-ui";

formatTimeAgo("2025-01-01 12:00:00");
// Returns: e.g., "3 days ago"

Output Rules

formatTimeAgo outputs the corresponding format based on the difference between the current time and the target time:

Difference RangeOutput Example
< 60 seconds10 seconds ago
< 1 hour5 minutes ago
< 24 hours3 hours ago
< 7 days2 days ago
< 30 days1 week ago
< 365 days2 months ago
≥ 365 days1 year ago
Future / InvalidJust now or empty str

API

useTimeAgo(date, updateInterval?)

Reactive version: Automatically refreshes the calculation results

ParameterTypeDefaultDescription
date`MaybeRef<stringnumberDate>`
updateIntervalnumber30000Auto-refresh interval (milliseconds)

Returns: ComputedRef<string>

Updates automatically when the time changes, the date changes, or the refresh interval is reached.


formatTimeAgo(date)

Non-reactive version

ParameterTypeDefaultDescription
date`stringnumberDate`

Returns: string

Liao ICP No. 2025070134