Skip to content

Function: formatDateTime() ​

ts
function formatDateTime(value, options?): string;

Format a date using the browser's locale-aware Intl formatter. 使用浏览器本地化的 Intl 格式器格式化日期时间。

Accepts strings, numbers (ms timestamps), Date, or nullish. Returns fallback (default '') when input is null/undefined or produces an invalid date.

Parameters ​

ParameterTypeDescription
valueDateRangeInput | null | undefinedA Date, string, or ms timestamp; nullish returns fallback / 日期、字符串或毫秒时间戳;空值返回 fallback
options?FormatDateTimeOptionsdateStyle (default 'short'), timeStyle (default 'medium', 'none' for date-only), fallback (default '')

Returns ​

string

The formatted date string, or fallback on invalid input / 格式化后的字符串,输入非法时返回 fallback

Example ​

ts
formatDateTime(new Date('2024-01-15T10:30:00')) // => '1/15/24, 10:30:00 AM' (locale-dependent)
formatDateTime('2024-01-15T10:30:00', { timeStyle: 'none' }) // => '1/15/24'
formatDateTime(null, { fallback: '-' }) // => '-'

Since ​

1.4.0

Released under the MIT License.