Skip to content

Variable: parse

ts
const parse: (input, format?) => Date | null = parseDate;

Parse various date inputs into a Date object. 将字符串、时间戳(毫秒)或 Date 解析为 Date 对象,解析失败返回 null。

  • string — tries native parsing, then common format patterns; pass format for exact matching
  • number — treated as milliseconds (JS standard); use parseUnix for seconds
  • Date — returns a clone

Parameters

ParameterType
inputstring | number | Date
format?string

Returns

Date | null

Example

ts
parseDate('2024-01-15') // Date
parseDate(1705276800000) // Date from ms timestamp
parseDate('25/12/2023', 'DD/MM/YYYY') // Date with explicit format

Since

1.0.0

Released under the MIT License.