/** * 时间格式化 * * @param time 时间 * @param fmt 格式 * @returns 格式化后的时间 */ const formatTimestamp = (timestamp: number, fmt = 'yyyy-MM-dd HH:mm:ss'): string => { if (!timestamp) { return '' } const time = new Date(timestamp) const dict: { [key: string]: any } = { yyyy: time.getFullYear(), M: time.getMonth() + 1, d: time.getDate(), H: time.getHours(), m: time.getMinutes(), s: time.getSeconds(), w: time.getDay(), MM: ('' + (time.getMonth() + 101)).substring(1), dd: ('' + (time.getDate() + 100)).substring(1), HH: ('' + (time.getHours() + 100)).substring(1), mm: ('' + (time.getMinutes() + 100)).substring(1), ss: ('' + (time.getSeconds() + 100)).substring(1), SSS: ('' + (time.getMilliseconds() + 1000)).substring(1), WW: getWeek(time.getDay()) } return fmt.replace(/(yyyy|MM?|dd?|HH?|mm?|ss?|SSS?|WW?)/g, function () { // eslint-disable-next-line prefer-rest-params return dict[arguments[0]] }) } /** * 获取大写周数 * * @param week 一周中的第几天 * @returns 大写周数 */ const getWeek = (week: number): string => { switch (week) { case 0: return '日' case 1: return '一' case 2: return '二' case 3: return '三' case 4: return '四' case 5: return '五' case 6: return '六' default: return '' } } export { formatTimestamp, getWeek }