Function formatDate

按指定格式格式化时间

支持传入时间戳、日期字符串或 Date 对象,并根据格式模板输出字符串。

如果输入的时间无效,会抛出错误

formatDate(1714298400000);
// 输出 "2025-04-28 18:00:00"
formatDate("2025-04-28T10:00:00Z", "yyyy/MM/dd");
// 输出 "2025/04/28"
formatDate(new Date(), "HH:mm:ss");
// 输出 "18:00:00"
  • Parameters

    • timestamp: string | number | Date

      要格式化的时间,可以是字符串、时间戳或 Date 实例

    • Optionalformat: string = 'yyyy-MM-dd HH:mm:ss'

      格式化模板,支持的占位符包括: yyyy, yy, MM, M, dd, d, HH, H, hh, h, mm, m, ss, s

    Returns string

    格式化后的时间字符串