feat: Performance timeLabel done.
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
* @param {number} Second
|
||||
* @returns {string} 轉換完的格式(ex: 1 day, 6.8 hrs)
|
||||
*/
|
||||
export default function TimeLabel(Second) {
|
||||
export function getTimeLabel(Second) {
|
||||
var day = 24 * 60 * 60
|
||||
var hour = 60 * 60
|
||||
var minutes = 60
|
||||
@@ -24,3 +24,65 @@ export default function TimeLabel(Second) {
|
||||
}
|
||||
return Second + " secs"
|
||||
}
|
||||
/**
|
||||
* 將秒數轉換成帶有縮寫時間單位的格式
|
||||
* @param {number} second
|
||||
* @param {number} fixedNumber 小數點後幾位
|
||||
* @returns {string} 轉換完的格式(ex: 1 d, 6.8 h)
|
||||
*/
|
||||
export function simpleTimeLabel(second, fixedNumber = 0) {
|
||||
const day = 24 * 60 * 60;
|
||||
const hour = 60 * 60;
|
||||
const minutes = 60;
|
||||
const dd = Math.floor(second / day);
|
||||
const hh = Math.floor((second % day) / hour);
|
||||
const mm = Math.floor((second % hour) / minutes);
|
||||
|
||||
if(dd > 0){
|
||||
return (second / day).toFixed(fixedNumber) + "d"
|
||||
}
|
||||
else if(hh > 0){
|
||||
return ((second % day) / hour).toFixed(fixedNumber) + "h"
|
||||
}
|
||||
else if(mm > 0){
|
||||
return ((second % hour) / minutes).toFixed(fixedNumber) + "m"
|
||||
}
|
||||
if(second == 0){
|
||||
return second + "s"
|
||||
}
|
||||
return second + "s"
|
||||
}
|
||||
/**
|
||||
* 跟隨最大值的時間單位,將秒數轉換成帶有縮寫時間單位的格式
|
||||
* @param {number} second 要轉換單位的秒數
|
||||
* @param {number} max 該 data 中的最大值
|
||||
* @param {number} fixedNumber 小數點後幾位
|
||||
* @returns {string} 轉換完的格式(ex: 1 d, 6.8 h)
|
||||
*/
|
||||
export function followTimeLabel(second, max, fixedNumber = 0) {
|
||||
const day = 24 * 60 * 60;
|
||||
const hour = 60 * 60;
|
||||
const minutes = 60;
|
||||
const dd = Math.floor(max / day);
|
||||
const hh = Math.floor((max % day) / hour);
|
||||
const mm = Math.floor((max % hour) / minutes);
|
||||
let maxUnit = '';
|
||||
let result;
|
||||
|
||||
maxUnit = dd > 0 ? 'd' : hh > 0 ? 'h' : mm > 0 ? 'm' : 's';
|
||||
switch (maxUnit) {
|
||||
case 'd':
|
||||
result = (second / day).toFixed(fixedNumber) + 'd';
|
||||
break;
|
||||
case 'h':
|
||||
result = ((second % day) / hour).toFixed(fixedNumber) + 'h';
|
||||
break;
|
||||
case 'm':
|
||||
result = ((second % hour) / minutes).toFixed(fixedNumber) + 'm';
|
||||
break;
|
||||
case 's':
|
||||
result = second.toFixed(fixedNumber) + 's';
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user