import { isDate } from './isDate'; export const dateFormat = (date: string | Date | number, format: string) => { if (!isDate(date)) return null; // 时间格式化函数 let time: Date | number; if (!isNaN(date as any)) { date = Number(date); } else if (typeof date === 'string' && date.includes('-')) { date = date.replace(/-/g, '/'); } time = new Date(date); const o: Record = { YYYY: time.getFullYear(), YY: `${time.getFullYear()}`.slice(-2), MM: `0${time.getMonth() + 1}`.slice(-2), M: time.getMonth() + 1, DD: `0${time.getDate()}`.slice(-2), D: time.getDate(), HH: `0${time.getHours()}`.slice(-2), H: time.getHours(), hh: `0${time.getHours() % 12}`.slice(-2), h: time.getHours() % 12, mm: `0${time.getMinutes()}`.slice(-2), m: time.getMinutes(), ss: `0${time.getSeconds()}`.slice(-2), s: time.getSeconds(), w: (function () { return ['日', '一', '二', '三', '四', '五', '六'][time.getDay()]; })(), }; for (const k in o) { format = format.replace(k, o[k]); } return format; };