112 lines
3.2 KiB
JavaScript
112 lines
3.2 KiB
JavaScript
// utils/timeFormat.js
|
||
|
||
/**
|
||
* 时间格式化工具函数
|
||
* 支持多种时间格式输入和多种输出格式
|
||
*/
|
||
|
||
/**
|
||
* 格式化时间为指定格式
|
||
* @param {string|number|Date} time - 时间输入(时间戳、日期字符串或Date对象)
|
||
* @param {string} format - 输出格式,默认:YYYY-MM-DD HH:mm:ss
|
||
* @returns {string} 格式化后的时间字符串
|
||
*/
|
||
export function formatTime(time, format = 'YYYY-MM-DD HH:mm:ss') {
|
||
if (!time) return '未知时间';
|
||
|
||
let date;
|
||
if (time instanceof Date) {
|
||
date = time;
|
||
} else if (typeof time === 'number') {
|
||
date = new Date(time);
|
||
} else {
|
||
// 修复1:仅替换日期分隔符,保留秒和时区信息
|
||
const sanitized = time
|
||
.replace(/(\d{4})-(\d{2})-(\d{2})/, '$1/$2/$3') // 只替换日期部分的短横线
|
||
.replace(/\.\d{3}/, ''); // 精确移除毫秒(保留秒)
|
||
|
||
date = new Date(sanitized);
|
||
|
||
// 修复2:如果解析失败,尝试直接解析原始字符串
|
||
if (isNaN(date.getTime())) {
|
||
date = new Date(time);
|
||
}
|
||
}
|
||
|
||
if (isNaN(date.getTime())) {
|
||
console.error('解析失败的时间:', time); // 调试日志
|
||
return '无效时间';
|
||
}
|
||
|
||
// 原有格式化代码保持不变
|
||
const year = date.getFullYear();
|
||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||
const day = String(date.getDate()).padStart(2, '0');
|
||
const hour = String(date.getHours()).padStart(2, '0');
|
||
const minute = String(date.getMinutes()).padStart(2, '0');
|
||
const second = String(date.getSeconds()).padStart(2, '0');
|
||
|
||
return format
|
||
.replace('YYYY', year)
|
||
.replace('MM', month)
|
||
.replace('DD', day)
|
||
.replace('HH', hour)
|
||
.replace('mm', minute)
|
||
.replace('ss', second);
|
||
}
|
||
/**
|
||
* 格式化时间为相对时间(如:5分钟前)
|
||
* @param {string|number|Date} time - 时间输入
|
||
* @returns {string} 相对时间字符串
|
||
*/
|
||
export function formatRelativeTime(time) {
|
||
if (!time) return '未知时间';
|
||
|
||
|
||
|
||
const now = new Date();
|
||
const date = new Date(time);
|
||
const diff = now - date;
|
||
|
||
const minute = 1000 * 60;
|
||
const hour = minute * 60;
|
||
const day = hour * 24;
|
||
const month = day * 30;
|
||
const year = day * 365;
|
||
|
||
if (diff < minute) {
|
||
return Math.floor(diff / 1000) + '秒前';
|
||
} else if (diff < hour) {
|
||
return Math.floor(diff / minute) + '分钟前';
|
||
} else if (diff < day) {
|
||
return Math.floor(diff / hour) + '小时前';
|
||
} else if (diff < month) {
|
||
return Math.floor(diff / day) + '天前';
|
||
} else if (diff < year) {
|
||
return Math.floor(diff / month) + '月前';
|
||
} else {
|
||
return Math.floor(diff / year) + '年前';
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取两个时间之间的差值
|
||
* @param {string|number|Date} startTime - 开始时间
|
||
* @param {string|number|Date} endTime - 结束时间
|
||
* @param {string} unit - 时间单位('second', 'minute', 'hour', 'day')
|
||
* @returns {number} 时间差值
|
||
*/
|
||
export function getTimeDiff(startTime, endTime, unit = 'minute') {
|
||
const start = new Date(startTime);
|
||
const end = new Date(endTime);
|
||
const diff = end - start;
|
||
|
||
const units = {
|
||
'second': 1000,
|
||
'minute': 1000 * 60,
|
||
'hour': 1000 * 60 * 60,
|
||
'day': 1000 * 60 * 60 * 24
|
||
};
|
||
|
||
return Math.floor(diff / units[unit] || 0);
|
||
} |