function DateFormat(date, format) { if (!date) { date = new Date(); } if (!format) { format = 'yyyy-MM-dd HH:mm:ss'; } // 处理参数默认值 if (typeof date === 'string' || typeof date === 'number') { date = new Date(date); } date = date instanceof Date ? date : new Date(); format = format || 'yyyy-MM-dd'; // 检查日期是否有效 if (isNaN(date.getTime())) { return 'Invalid Date'; } // 定义格式化映射 const formatMap = { 'yyyy': date.getFullYear(), 'MM': String(date.getMonth() + 1).padStart(2, '0'), 'dd': String(date.getDate()).padStart(2, '0'), 'HH': String(date.getHours()).padStart(2, '0'), 'mm': String(date.getMinutes()).padStart(2, '0'), 'ss': String(date.getSeconds()).padStart(2, '0'), 'SSS': String(date.getMilliseconds()).padStart(3, '0'), 'M': date.getMonth() + 1, 'd': date.getDate(), 'H': date.getHours(), 'm': date.getMinutes(), 's': date.getSeconds(), 'S': date.getMilliseconds() }; // 替换格式字符串中的占位符 return format.replace(/(yyyy|MM|dd|HH|mm|ss|SSS|M|d|H|m|s|S)/g, (match) => { return formatMap[match]; }); } export default{ DateFormat:DateFormat }