Skip to content

@pmun/utils / formatHumanReadable

Function: formatHumanReadable()

ts
function formatHumanReadable(date): string;

格式化日期为人类友好的格式

Parameters

date

DateLike

日期

Returns

string

人类友好的日期描述,如"今天 HH:mm"、"昨天 HH:mm"、"明天 HH:mm"或"YYYY-MM-DD HH:mm"

Example

ts
formatHumanReadable(new Date()) // "今天 15:30"
formatHumanReadable(new Date(Date.now() - 24 * 60 * 60 * 1000)) // "昨天 15:30"
formatHumanReadable(new Date(Date.now() + 24 * 60 * 60 * 1000)) // "明天 15:30"
formatHumanReadable('2023-01-01') // "2023-01-01 00:00"