fix: #302. Shouldn't use mod and shouldn't double divide.

This commit is contained in:
Cindy Chang
2024-06-17 23:03:46 +08:00
parent cee61dbd93
commit 9b88de4c3f
3 changed files with 18 additions and 19 deletions

View File

@@ -39,13 +39,12 @@ const getTimeUnitAndValueToUse = (secondToDecide) => {
timeValue: secondToDecide / hour,
};
} else if (mm > 0 && mm > 1) {
console.log('mm > 0 && mm > 1', secondToDecide / minutes);
return {
unitToUse: "m",
timeValue: secondToDecide / minutes,
};
} else {
console.log('less than one minutes final case timeValue:', secondToDecide, dd, hh, mm); //TODO:
console.log('less than one minutes final case timeValue:', secondToDecide, dd, hh, mm);
return {
unitToUse: "s",
timeValue: secondToDecide,
@@ -161,16 +160,16 @@ export function followTimeLabel(second, max, fixedNumber = 0) {
const minutes = 60;
const dd = max / day;
const hh = (max % day) / hour;
const mm = (max % hour) / minutes;
const hh = max / hour;
const mm = max/ minutes;
let maxUnit = '';
let result = "";
if (dd > 0) {
if (dd > 1) {
maxUnit = 'd';
} else if (hh > 0) {
} else if (hh > 1) {
maxUnit = 'h';
} else if (mm > 0) {
} else if (mm > 1) {
maxUnit = 'm';
} else {
maxUnit = 's';
@@ -186,13 +185,13 @@ export function followTimeLabel(second, max, fixedNumber = 0) {
if(((second % day) / hour) === 0) {
fixedNumber = 0;
}
result = ((second % day) / hour).toFixed(fixedNumber) + 'h';
result = (second / hour).toFixed(fixedNumber) + 'h';
break;
case 'm':
if(((second % hour) / minutes) === 0) {
if((second / minutes) === 0) {
fixedNumber = 0;
}
result = ((second % hour) / minutes).toFixed(fixedNumber) + 'm';
result = (second / minutes).toFixed(fixedNumber) + 'm';
break;
case 's':
if(second === 0) {
@@ -211,7 +210,7 @@ export function followTimeLabel(second, max, fixedNumber = 0) {
* @param { string } maxX 該 data 最大的日期,格式是 timestamp
* @param { string } minX 該 data 最小的日期,格式是 timestamp
*/
export function dateLabel(date, maxDate, minDate) {
export function getDateLabelByMinMaxDate(date, maxDate, minDate) {
// 將時間字串轉換為時間物件
// new Date(time) 之後不用 getTime()因為在JavaScript中
// 日期物件是以時間戳記timestamp的形式存儲的。當創建一個新的日期物件時
@@ -238,16 +237,16 @@ export function dateLabel(date, maxDate, minDate) {
const diffSeconds = Math.floor(timeDiff);
// 顯示結果
if (diffMonths > 0) {
if (diffMonths > 1) {
return moment(date).format('YYYY/MM/DD');
}
else if (diffDays > 0) {
else if (diffDays > 1) {
return moment(date).format('MM/DD');
}
else if (diffHours > 0) {
else if (diffHours > 1) {
return moment(date).format('MM/DD hh:00');
}
else if (diffMinutes > 0) {
else if (diffMinutes > 1) {
return moment(date).format('MM/DD hh:mm');
}
else {