修复 时间 格式化为题
This commit is contained in:
@@ -78,4 +78,108 @@ export const getTime = (time: string): string => {
|
||||
const minuteStr = minute.toString().padStart(2, '0')
|
||||
|
||||
return `${hour12}:${minuteStr} ${period}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间显示(相对时间)
|
||||
* @param timeStr 时间字符串
|
||||
* @returns 格式化后的时间字符串
|
||||
*/
|
||||
export const formatRelativeTime = (timeStr: string): string => {
|
||||
if (!timeStr) return "";
|
||||
|
||||
const date = new Date(timeStr);
|
||||
const now = new Date();
|
||||
|
||||
// 获取日期部分(年-月-日),忽略时间
|
||||
const getDateString = (d: Date) => {
|
||||
const year = d.getFullYear();
|
||||
const month = d.getMonth() + 1;
|
||||
const day = d.getDate();
|
||||
return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
|
||||
};
|
||||
|
||||
const dateStr = getDateString(date);
|
||||
const nowStr = getDateString(now);
|
||||
|
||||
// 计算日期差
|
||||
const dateObj = new Date(dateStr);
|
||||
const nowObj = new Date(nowStr);
|
||||
const diffTime = nowObj.getTime() - dateObj.getTime();
|
||||
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
|
||||
|
||||
if (diffDays === 0) {
|
||||
// 今天
|
||||
const hours = date.getHours();
|
||||
const minutes = date.getMinutes();
|
||||
return `今天 ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
|
||||
} else if (diffDays === 1) {
|
||||
// 昨天
|
||||
const hours = date.getHours();
|
||||
const minutes = date.getMinutes();
|
||||
return `昨天 ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
|
||||
} else if (diffDays < 7) {
|
||||
// 一周内显示天数
|
||||
return `${diffDays}天前`;
|
||||
} else {
|
||||
// 超过一周显示完整日期
|
||||
const year = date.getFullYear();
|
||||
const month = date.getMonth() + 1;
|
||||
const day = date.getDate();
|
||||
const hours = date.getHours();
|
||||
const minutes = date.getMinutes();
|
||||
return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间显示(简短相对时间)
|
||||
* @param timeStr 时间字符串
|
||||
* @returns 格式化后的时间字符串
|
||||
*/
|
||||
export const formatShortRelativeTime = (timeStr: string): string => {
|
||||
if (!timeStr) return "";
|
||||
|
||||
const date = new Date(timeStr);
|
||||
const now = new Date();
|
||||
|
||||
// 获取日期部分(年-月-日),忽略时间
|
||||
const getDateString = (d: Date) => {
|
||||
const year = d.getFullYear();
|
||||
const month = d.getMonth() + 1;
|
||||
const day = d.getDate();
|
||||
return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
|
||||
};
|
||||
|
||||
const dateStr = getDateString(date);
|
||||
const nowStr = getDateString(now);
|
||||
|
||||
// 计算日期差
|
||||
const dateObj = new Date(dateStr);
|
||||
const nowObj = new Date(nowStr);
|
||||
const diffTime = nowObj.getTime() - dateObj.getTime();
|
||||
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
|
||||
|
||||
// 如果是今天,显示具体时间
|
||||
if (diffDays === 0) {
|
||||
const diff = now.getTime() - date.getTime();
|
||||
const minutes = Math.floor(diff / (1000 * 60));
|
||||
const hours = Math.floor(diff / (1000 * 60 * 60));
|
||||
|
||||
if (minutes < 1) {
|
||||
return "刚刚";
|
||||
} else if (minutes < 60) {
|
||||
return `${minutes}分钟前`;
|
||||
} else {
|
||||
return `${hours}小时前`;
|
||||
}
|
||||
} else if (diffDays === 1) {
|
||||
return "1天前";
|
||||
} else if (diffDays < 7) {
|
||||
return `${diffDays}天前`;
|
||||
} else {
|
||||
const month = date.getMonth() + 1;
|
||||
const day = date.getDate();
|
||||
return `${month}月${day}日`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user