完善请求
This commit is contained in:
@@ -106,24 +106,31 @@ const tableData = reactive<TableState>({
|
||||
* @param time 时间字符串或时间戳
|
||||
* @returns 格式化后的时间字符串
|
||||
*/
|
||||
const formatTime = (time: string | number): string => {
|
||||
const formatTime = (time: string | number | Date): string => {
|
||||
if (!time) return '-';
|
||||
|
||||
try {
|
||||
// 如果是时间戳格式
|
||||
let timestamp = typeof time === 'string' ? parseInt(time) : time;
|
||||
let date: Date;
|
||||
|
||||
// 如果是毫秒时间戳,需要判断是否需要转换
|
||||
if (timestamp > 1000000000000) {
|
||||
// 已经是毫秒时间戳
|
||||
} else if (timestamp > 1000000000) {
|
||||
// 秒时间戳,转换为毫秒
|
||||
timestamp = timestamp * 1000;
|
||||
if (time instanceof Date) {
|
||||
date = time;
|
||||
} else if (typeof time === 'string') {
|
||||
// 直接使用字符串创建Date对象,ISO格式会自动识别
|
||||
date = new Date(time);
|
||||
} else {
|
||||
// 处理时间戳
|
||||
let timestamp = time;
|
||||
if (timestamp > 1000000000000) {
|
||||
// 已经是毫秒时间戳
|
||||
} else if (timestamp > 1000000000) {
|
||||
// 秒时间戳,转换为毫秒
|
||||
timestamp = timestamp * 1000;
|
||||
}
|
||||
date = new Date(timestamp);
|
||||
}
|
||||
|
||||
const date = new Date(timestamp);
|
||||
if (isNaN(date.getTime())) {
|
||||
return String(time); // 返回原始值
|
||||
return String(time);
|
||||
}
|
||||
|
||||
const year = date.getFullYear();
|
||||
@@ -136,7 +143,7 @@ const formatTime = (time: string | number): string => {
|
||||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
||||
} catch (error) {
|
||||
console.error('时间格式化错误:', error);
|
||||
return String(time); // 返回原始值
|
||||
return String(time);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user