Skip to content

useTimeAgo

Category
Export Size
1.42 kB
Last Changed
last week

Reactive time ago. Automatically update the time ago string when the time changes.

Demo

Usage

ts
import { 
useTimeAgo
} from '@vueuse/core'
const
timeAgo
=
useTimeAgo
(new
Date
(2021, 0, 1))

Component Usage

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.

vue
<template>
  <UseTimeAgo v-slot="{ 
timeAgo
}"
:time
="new
Date
(2021, 0, 1)">
Time Ago: {{
timeAgo
}}
</UseTimeAgo> </template>

Non-Reactivity Usage

In case you don't need the reactivity, you can use the formatTimeAgo function to get the formatted string instead of a Ref.

ts
import { 
formatTimeAgo
} from '@vueuse/core'
const
timeAgo
=
formatTimeAgo
(new
Date
(2021, 0, 1)) // string

Type Declarations

Show Type Declarations
ts
export type 
UseTimeAgoFormatter
<
T
= number> = (
value
:
T
,
isPast
: boolean,
) => string export type
UseTimeAgoUnitNamesDefault
=
| "second" | "minute" | "hour" | "day" | "week" | "month" | "year" export interface UseTimeAgoMessagesBuiltIn {
justNow
: string
past
: string |
UseTimeAgoFormatter
<string>
future
: string |
UseTimeAgoFormatter
<string>
invalid
: string
} export type
UseTimeAgoMessages
<
UnitNames
extends string =
UseTimeAgoUnitNamesDefault
,
> = UseTimeAgoMessagesBuiltIn &
Record
<
UnitNames
, string |
UseTimeAgoFormatter
<number>>
export interface
FormatTimeAgoOptions
<
UnitNames
extends string =
UseTimeAgoUnitNamesDefault
,
> { /** * Maximum unit (of diff in milliseconds) to display the full date instead of relative * * @default undefined */
max
?:
UnitNames
| number
/** * Formatter for full date */
fullDateFormatter
?: (
date
: Date) => string
/** * Messages for formatting the string */
messages
?:
UseTimeAgoMessages
<
UnitNames
>
/** * Minimum display time unit (default is minute) * * @default false */
showSecond
?: boolean
/** * Rounding method to apply. * * @default 'round' */
rounding
?: "round" | "ceil" | "floor" | number
/** * Custom units */
units
?:
UseTimeAgoUnit
<
UnitNames
>[]
} export interface
UseTimeAgoOptions
<
Controls
extends boolean,
UnitNames
extends string =
UseTimeAgoUnitNamesDefault
,
> extends
FormatTimeAgoOptions
<
UnitNames
> {
/** * Expose more controls * * @default false */
controls
?:
Controls
/** * Intervals to update, set 0 to disable auto update * * @default 30_000 */
updateInterval
?: number
} export interface
UseTimeAgoUnit
<
Unit
extends string =
UseTimeAgoUnitNamesDefault
,
> {
max
: number
value
: number
name
:
Unit
} export type
UseTimeAgoReturn
<
Controls
extends boolean = false> =
Controls
extends true
? {
timeAgo
:
ComputedRef
<string>
} &
Pausable
:
ComputedRef
<string>
/** * Reactive time ago formatter. * * @see https://vueuse.org/useTimeAgo * * @__NO_SIDE_EFFECTS__ */ export declare function
useTimeAgo
<
UnitNames
extends string =
UseTimeAgoUnitNamesDefault
,
>(
time
:
MaybeRefOrGetter
<Date | number | string>,
options
?:
UseTimeAgoOptions
<false,
UnitNames
>,
):
UseTimeAgoReturn
<false>
export declare function
useTimeAgo
<
UnitNames
extends string =
UseTimeAgoUnitNamesDefault
,
>(
time
:
MaybeRefOrGetter
<Date | number | string>,
options
:
UseTimeAgoOptions
<true,
UnitNames
>,
):
UseTimeAgoReturn
<true>
export declare function
formatTimeAgo
<
UnitNames
extends string =
UseTimeAgoUnitNamesDefault
,
>(
from
: Date,
options
?:
FormatTimeAgoOptions
<
UnitNames
>,
now
?: Date | number,
): string

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
SerKo
Robin
IlyaL
OrbisK
Nick Messing
WORMSS
vaakian X
Demian
Joaquín Sánchez
Connor 'Birb' McCormick
836334258
sun0day
azaleta
vaakian X
Jelf
wheat
Alex Kozack

Changelog

v13.6.0 on
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.7.2 on
1d6be - fix: Support custom UnitNames type for units field (#3684)
v10.0.0-beta.4 on
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue

Released under the MIT License.

Build faster with AI
New Masterclass to help you leverage AI in your Vue workflow
Get Early Access