add setTimeStringFormatBaseOnTimeDifference

This commit is contained in:
Cindy Chang
2024-06-07 08:54:21 +08:00
parent c0c455c55c
commit 29675af82b

View File

@@ -185,3 +185,33 @@ export function dateLabel(date, maxDate, minDate) {
else if (diffMinutes > 0) return moment(date).format('MM/DD hh:mm'); else if (diffMinutes > 0) return moment(date).format('MM/DD hh:mm');
else return moment(date).format('hh:mm:ss'); else return moment(date).format('hh:mm:ss');
} }
/**
* Select an appropriate time format based on the time difference.
* 根據時間差距選擇適合的時間格式
* 月: 2022/06
* 日: 06/06
* 時: 03/05 12:00
* 分: 03/05 12:15
* 秒: 09:05:32
* @param {Array<number>} timestamps - An array of timestamps.
* @returns {string} - The suitable time format string.
*/
export const setTimeStringFormatBaseOnTimeDifference = (minTimeStamp, maxTimeStamp) => {
const timeDifferenceInSeconds = (maxTimestamp - minTimestamp) / 1000;
let dateFormat;
if (timeDifferenceInSeconds < 60) {
dateFormat = 'HH:mm:ss'; // 秒
} else if (timeDifferenceInSeconds < 3600) {
dateFormat = 'MM/DD HH:mm'; // 分鐘
} else if (timeDifferenceInSeconds < 86400) { // 86400 秒 = 24 小時
dateFormat = 'MM/DD HH:mm'; // 小時
} else if (timeDifferenceInSeconds < 2592000) { // 2592000 秒 = 30 天
dateFormat = 'MM/DD'; // 天
} else {
dateFormat = 'YYYY/MM'; // 月
}
return dateFormat;
};