Skip to content

useTimeAgo

反应式时间之前。当时间改变时自动更新 time ago 字符串。

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

示例

用法

¥Usage

js
import { useTimeAgo } from '@vueuse/core'

const timeAgo = useTimeAgo(new Date(2021, 0, 1))

组件用法

¥Component Usage

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

非反应式用法

¥Non-Reactivity Usage

如果你不需要反应性,可以使用 formatTimeAgo 函数来获取格式化字符串而不是 Ref。

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

js
import { formatTimeAgo } from '@vueuse/core'

const timeAgo = formatTimeAgo(new Date(2021, 0, 1)) // string