跳到主要内容

useTimeAgoIntl

分类
导出大小
上次更改
2 个月前

支持国际化的响应式时间间隔。当时间改变时自动更新时间间隔字符串。由 Intl.RelativeTimeFormat 提供支持。

演示

English: now, Chinese: 现在
0毫秒

用法

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

const timeAgoIntl = useTimeAgoIntl(new Date(2021, 0, 1))

非响应式用法

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

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

const timeAgoIntl = formatTimeAgoIntl(new Date(2021, 0, 1)) // string

类型声明

显示类型声明
ts
type 
Locale
= Intl.
UnicodeBCP47LocaleIdentifier
| Intl.
Locale
export interface FormatTimeAgoIntlOptions { /** * The locale to format with * * @default undefined * @see https://mdn.org.cn/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#locales */
locale
?:
Locale
/** * @see https://mdn.org.cn/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#options */
relativeTimeFormatOptions
?: Intl.
RelativeTimeFormatOptions
/** * Whether to insert spaces between parts. * * Ignored if `joinParts` is provided. * * @default true */
insertSpace
?: boolean
/** * Custom function to join the parts returned by `Intl.RelativeTimeFormat.formatToParts`. * * If provided, it will be used instead of the default join logic. */
joinParts
?: (
parts
: Intl.
RelativeTimeFormatPart
[],
locale
?: Intl.
UnicodeBCP47LocaleIdentifier
| Intl.
Locale
,
) => string } export interface
UseTimeAgoIntlOptions
<
Controls
extends boolean>
extends FormatTimeAgoIntlOptions { /** * Expose more controls and the raw `parts` result. * * @default false */
controls
?:
Controls
/** * Update interval in milliseconds, set 0 to disable auto update * * @default 30_000 */
updateInterval
?: number
} type
UseTimeAgoReturn
<
Controls
extends boolean = false> =
Controls
extends true
? {
timeAgoIntl
:
ComputedRef
<string>
parts
:
ComputedRef
<Intl.
RelativeTimeFormatPart
[]>
} &
Pausable
:
ComputedRef
<string>
export interface TimeAgoUnit {
name
: Intl.
RelativeTimeFormatUnit
ms
: number
} /** * A reactive wrapper for `Intl.RelativeTimeFormat`. */ export declare function
useTimeAgoIntl
(
time
:
MaybeRefOrGetter
<Date | number | string>,
options
?:
UseTimeAgoIntlOptions
<false>,
):
UseTimeAgoReturn
<false>
export declare function
useTimeAgoIntl
(
time
:
MaybeRefOrGetter
<Date | number | string>,
options
:
UseTimeAgoIntlOptions
<true>,
):
UseTimeAgoReturn
<true>
/** * Non-reactive version of useTimeAgoIntl */ export declare function
formatTimeAgoIntl
(
from
: Date,
options
?: FormatTimeAgoIntlOptions,
now
?: Date | number,
): string /** * Format parts into a string */ export declare function
formatTimeAgoIntlParts
(
parts
: Intl.
RelativeTimeFormatPart
[],
options
?: FormatTimeAgoIntlOptions,
): string

来源

源代码演示文档

贡献者

Northword

更新日志

v13.7.0
81815 - 特性: 添加 useTimaAgoIntl (#4821)

根据 MIT 许可证发布。