修改 列表 时间格式化问题
This commit is contained in:
@@ -139,10 +139,10 @@ export const formatRelativeTime = (timeStr: string): string => {
|
||||
*/
|
||||
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();
|
||||
@@ -150,10 +150,10 @@ export const formatShortRelativeTime = (timeStr: string): string => {
|
||||
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);
|
||||
@@ -165,7 +165,7 @@ export const formatShortRelativeTime = (timeStr: string): string => {
|
||||
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) {
|
||||
@@ -182,4 +182,114 @@ export const formatShortRelativeTime = (timeStr: string): string => {
|
||||
const day = date.getDate();
|
||||
return `${month}月${day}日`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化球局时间显示(例如:明天(周五)下午5点)
|
||||
* @param timeStr 时间字符串
|
||||
* @returns 格式化后的时间字符串
|
||||
*/
|
||||
export const formatGameTime = (timeStr: string): string => {
|
||||
if (!timeStr) return "";
|
||||
|
||||
const date = new Date(timeStr);
|
||||
const now = new Date();
|
||||
|
||||
// 获取星期几
|
||||
const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
|
||||
const weekDay = weekDays[date.getDay()];
|
||||
|
||||
// 获取小时和分钟
|
||||
const hours = date.getHours();
|
||||
const minutes = date.getMinutes();
|
||||
|
||||
// 判断上午/下午/晚上
|
||||
let period = '';
|
||||
let displayHour = hours;
|
||||
|
||||
if (hours >= 0 && hours < 6) {
|
||||
period = '凌晨';
|
||||
} else if (hours >= 6 && hours < 12) {
|
||||
period = '上午';
|
||||
} else if (hours >= 12 && hours < 18) {
|
||||
period = '下午';
|
||||
displayHour = hours === 12 ? 12 : hours - 12;
|
||||
} else {
|
||||
period = '晚上';
|
||||
displayHour = hours - 12;
|
||||
}
|
||||
|
||||
// 格式化时间部分
|
||||
const timeStr2 = minutes === 0 ? `${displayHour}点` : `${displayHour}点${minutes}分`;
|
||||
|
||||
// 获取日期部分(年-月-日),忽略时间
|
||||
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 = dateObj.getTime() - nowObj.getTime();
|
||||
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
|
||||
|
||||
// 根据日期差返回不同格式
|
||||
if (diffDays === 0) {
|
||||
// 今天
|
||||
return `今天(${weekDay})${period}${timeStr2}`;
|
||||
} else if (diffDays === 1) {
|
||||
// 明天
|
||||
return `明天(${weekDay})${period}${timeStr2}`;
|
||||
} else if (diffDays === 2) {
|
||||
// 后天
|
||||
return `后天(${weekDay})${period}${timeStr2}`;
|
||||
} else if (diffDays > 2 && diffDays <= 7) {
|
||||
// 一周内
|
||||
return `${weekDay}${period}${timeStr2}`;
|
||||
} else {
|
||||
// 超过一周,显示具体日期
|
||||
const month = date.getMonth() + 1;
|
||||
const day = date.getDate();
|
||||
return `${month}月${day}日(${weekDay})${period}${timeStr2}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算时长(结束时间 - 开始时间)
|
||||
* @param startTime 开始时间字符串
|
||||
* @param endTime 结束时间字符串
|
||||
* @returns 格式化后的时长字符串(如:2小时、1.5小时、30分钟)
|
||||
*/
|
||||
export const calculateDuration = (startTime: string, endTime: string): string => {
|
||||
if (!startTime || !endTime) return "";
|
||||
|
||||
const start = new Date(startTime);
|
||||
const end = new Date(endTime);
|
||||
|
||||
// 计算时间差(毫秒)
|
||||
const diffMs = end.getTime() - start.getTime();
|
||||
|
||||
// 转换为分钟
|
||||
const diffMinutes = Math.floor(diffMs / (1000 * 60));
|
||||
|
||||
// 转换为小时
|
||||
const hours = Math.floor(diffMinutes / 60);
|
||||
const minutes = diffMinutes % 60;
|
||||
|
||||
// 格式化输出
|
||||
if (hours > 0 && minutes > 0) {
|
||||
return `${hours}.5小时`;
|
||||
} else if (hours > 0) {
|
||||
return `${hours}小时`;
|
||||
} else if (minutes > 0) {
|
||||
return `${minutes}分钟`;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user