Skip to content

Function: timeAgo() ​

ts
function timeAgo(date, options?): string;

Relative time from now to the past — e.g., "3 minutes ago" / "3 分钟前". 与现在的相对时间(面向过去),如 "3 分钟前"。

Accepts Date, string, number (ms timestamp), or nullish. Returns fallback (default '') when input is null/undefined or produces an invalid date.

Parameters ​

ParameterType
dateRelativeDateInput
optionsstring | RelativeOptions

Returns ​

string

Example ​

ts
timeAgo(new Date(Date.now() - 60000)) // '1 minute ago' or '1 分钟前'
timeAgo(null, 'zh-CN') // ''
timeAgo('invalid', { fallback: '-' }) // '-'

Since ​

1.0.0

Released under the MIT License.