WIP: refactoring Compare page

This commit is contained in:
Cindy Chang
2024-06-03 16:48:22 +08:00
parent 55aaab3672
commit 265680ae45
2 changed files with 221 additions and 178 deletions

View File

@@ -76,19 +76,27 @@ export function followTimeLabel(second, max, fixedNumber = 0) {
maxUnit = dd > 0 ? 'd' : hh > 0 ? 'h' : mm > 0 ? 'm' : 's';
switch (maxUnit) {
case 'd':
if((second / day) === 0) fixedNumber = 0;
if((second / day) === 0) {
fixedNumber = 0;
}
result = (second / day).toFixed(fixedNumber) + 'd';
break;
case 'h':
if(((second % day) / hour) === 0) fixedNumber = 0;
if(((second % day) / hour) === 0) {
fixedNumber = 0;
}
result = ((second % day) / hour).toFixed(fixedNumber) + 'h';
break;
case 'm':
if(((second % hour) / minutes) === 0) fixedNumber = 0;
if(((second % hour) / minutes) === 0) {
fixedNumber = 0;
}
result = ((second % hour) / minutes).toFixed(fixedNumber) + 'm';
break;
case 's':
if(second === 0) fixedNumber = 0;
if(second === 0) {
fixedNumber = 0;
}
result = second.toFixed(fixedNumber) + 's';
break;
}
@@ -113,18 +121,18 @@ export function dateLabel(date, maxDate, minDate) {
let timeDiff = maxDate - minDate;
// 計算相差的月份
var diffMonths = (maxDate.getFullYear() - minDate.getFullYear()) * 12;
let diffMonths = (maxDate.getFullYear() - minDate.getFullYear()) * 12;
diffMonths -= minDate.getMonth();
diffMonths += maxDate.getMonth();
// 計算相差的日期,要取整數才能接續下方的邏輯判斷 `diffDays > 0`
// 毫秒 * 秒 * 分鐘 * 小時 = 一天的時間量
var diffDays = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
const diffDays = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
// 計算相差的小時、分鐘、秒
var diffHours = Math.floor(timeDiff / (1000 * 60 * 60));
var diffMinutes = Math.floor(timeDiff / (1000 * 60));
var diffSeconds = Math.floor(timeDiff / 1000);
const diffHours = Math.floor(timeDiff / (1000 * 60 * 60));
const diffMinutes = Math.floor(timeDiff / (1000 * 60));
const diffSeconds = Math.floor(timeDiff / 1000);
// 顯示結果
if (diffMonths > 0) return moment(date).format('YYYY/MM/DD');