fix: 修改以支持非整数小时数

This commit is contained in:
2026-03-09 15:48:34 +08:00
parent aed3c4cc54
commit 243bb59c1d

View File

@@ -12,14 +12,28 @@ function genGameLength(startTime: Dayjs, endTime: Dayjs) {
if (!startTime || !endTime) {
return "";
}
const hours = endTime.diff(startTime, "hour");
if (Math.floor(hours / 24) >= 1) {
const leftHours = Math.floor(hours % 24);
return `${Math.floor(hours / 24)}${
leftHours !== 0 ? `${leftHours}小时` : ""
}`;
const totalMinutes = endTime.diff(startTime, "minute");
const totalHours = totalMinutes / 60;
if (totalHours >= 24) {
const days = Math.floor(totalHours / 24);
const remainingHours = totalHours % 24;
if (remainingHours === 0) {
return `${days}`;
}
// 保留一位小数
const displayHours = parseFloat(remainingHours.toFixed(1));
return `${days}${displayHours}小时`;
}
return `${hours}小时`;
// 如果是整数小时,不显示小数点
if (Number.isInteger(totalHours)) {
return `${totalHours}小时`;
}
// 保留一位小数去除末尾的0
return `${parseFloat(totalHours.toFixed(1))}小时`;
}
function genGameRange(startTime: Dayjs, endTime: Dayjs) {