Skip to content

useTimeAgo

Category
Export Size
1.63 kB
Last Changed
15 minutes ago

响应式的相对时间。当时间发生变化时,自动更新相对时间字符串。

Demo

just now
0ms

用法

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

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

组件用法

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

非响应式用法

如果您不需要响应性,可以使用 formatTimeAgo 函数获取格式化的字符串,而不是一个 Ref。

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

const timeAgo = formatTimeAgo(new Date(2021, 0, 1)) // 字符串

类型声明

显示类型声明
typescript
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,
> {
  /**
   * 最大单位(以毫秒为单位)在显示完整日期而不是相对时间之前显示
   *
   * @default undefined
   */
  max?: UnitNames | number
  /**
   * 完整日期的格式化程序
   */
  fullDateFormatter?: (date: Date) => string
  /**
   * 格式化字符串的消息
   */
  messages?: UseTimeAgoMessages<UnitNames>
  /**
   * 最小显示时间单位(默认为分钟)
   *
   * @default false
   */
  showSecond?: boolean
  /**
   * 应用的舍入方法。
   *
   * @default 'round'
   */
  rounding?: "round" | "ceil" | "floor" | number
  /**
   * 自定义单位
   */
  units?: UseTimeAgoUnit<UnitNames>[]
}
export interface UseTimeAgoOptions<
  Controls extends boolean,
  UnitNames extends string = UseTimeAgoUnitNamesDefault,
> extends FormatTimeAgoOptions<UnitNames> {
  /**
   * 公开更多控件
   *
   * @default false
   */
  controls?: Controls
  /**
   * 更新间隔,设置为 0 以禁用自动更新
   *
   * @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>
/**
 * 响应式的时间过去格式化工具。
 *
 * @see https://vueuse.org/useTimeAgo
 */
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

贡献者

Anthony Fu
丶远方
Anthony Fu
Nick Messing
WORMSS
vaakian X
Demian
Joaquín Sánchez
Connor 'Birb' McCormick
836334258
sun0day
azaleta
vaakian X
Jelf
wheat
Alex Kozack

变更日志

v10.7.2 on 1/14/2024
1d6be - fix: Support custom UnitNames type for units field (#3684)
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)
v9.9.0 on 12/23/2022
bb0fd - fix(formatTimeAgo): typo foramtTimeAgo -> formatTimeAgo (#2568)
v9.8.0 on 12/20/2022
f40a0 - fix: rounding unit fallback
9293c - feat: non-reactive version formatTimeAgo
v9.7.0 on 12/16/2022
324de - feat: custom units, number rounding
a7dc6 - feat: add floor and ceil value calculation (#2543)
0c333 - fix: add showSecond prop to component (#2547)

Released under the MIT License.