forked from dyf/dyf-vue-ui
113 lines
3.2 KiB
TypeScript
113 lines
3.2 KiB
TypeScript
/**
|
||
* 时间转换工具类
|
||
* 提供时间格式化、时间差计算等功能
|
||
*/
|
||
export class TimeConverter {
|
||
/**
|
||
* 检查时间字符串是否有效
|
||
* @param timeStr 时间字符串
|
||
* @returns 是否有效的时间
|
||
*/
|
||
static isValidTime(timeStr: string): boolean {
|
||
if (!timeStr) return false;
|
||
return !isNaN(new Date(timeStr).getTime());
|
||
}
|
||
|
||
/**
|
||
* 格式化时间
|
||
* @param timeStr 时间字符串
|
||
* @param format 格式,可选值: 'YYYY-MM-DD', 'HH:mm:ss', 'YYYY-MM-DD HH:mm:ss'
|
||
* @returns 格式化后的时间字符串
|
||
*/
|
||
static formatTime(timeStr: string, format: 'YYYY-MM-DD' | 'HH:mm:ss' | 'YYYY-MM-DD HH:mm:ss' = 'YYYY-MM-DD HH:mm:ss'): string {
|
||
if (!this.isValidTime(timeStr)) return '无效时间';
|
||
|
||
const date = new Date(timeStr);
|
||
const year = date.getFullYear();
|
||
const month = this.padZero(date.getMonth() + 1);
|
||
const day = this.padZero(date.getDate());
|
||
const hours = this.padZero(date.getHours());
|
||
const minutes = this.padZero(date.getMinutes());
|
||
const seconds = this.padZero(date.getSeconds());
|
||
|
||
switch (format) {
|
||
case 'YYYY-MM-DD':
|
||
return `${year}-${month}-${day}`;
|
||
case 'HH:mm:ss':
|
||
return `${hours}:${minutes}:${seconds}`;
|
||
case 'YYYY-MM-DD HH:mm:ss':
|
||
default:
|
||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 计算与当前时间的差值(格式为 HH:mm:ss)
|
||
* @param startTimeStr 起始时间字符串
|
||
* @returns 格式化的时间差字符串
|
||
*/
|
||
static calculateTimeDiff(startTimeStr: string): string {
|
||
if (!this.isValidTime(startTimeStr)) return '';
|
||
|
||
const startTime = new Date(startTimeStr).getTime();
|
||
const nowTime = new Date().getTime();
|
||
const diffMs = nowTime - startTime;
|
||
|
||
// 处理未来时间
|
||
if (diffMs < 0) {
|
||
return '00:00:00';
|
||
}
|
||
|
||
// 计算总秒数
|
||
const totalSeconds = Math.floor(diffMs / 1000);
|
||
|
||
// 计算时、分、秒
|
||
const hours = this.padZero(Math.floor(totalSeconds / 3600));
|
||
const minutes = this.padZero(Math.floor((totalSeconds % 3600) / 60));
|
||
const seconds = this.padZero(totalSeconds % 60);
|
||
|
||
return `${hours}:${minutes}:${seconds}`;
|
||
}
|
||
|
||
/**
|
||
* 数字补零
|
||
* @param num 数字
|
||
* @returns 补零后的字符串
|
||
*/
|
||
private static padZero(num: number): string {
|
||
return num < 10 ? `0${num}` : num.toString();
|
||
}
|
||
|
||
/**
|
||
* 创建一个时间差更新定时器
|
||
* @param startTimeStr 起始时间字符串
|
||
* @param callback 回调函数,接收计算后的时间差
|
||
* @param interval 刷新间隔(毫秒)
|
||
* @returns 定时器ID,用于清除定时器
|
||
*/
|
||
static createTimeDiffTimer(
|
||
startTimeStr: string,
|
||
callback: (diffText: string) => void,
|
||
interval: number = 1000
|
||
): number {
|
||
// 立即执行一次
|
||
callback(this.calculateTimeDiff(startTimeStr));
|
||
|
||
// 设置定时器
|
||
const timerId = window.setInterval(() => {
|
||
callback(this.calculateTimeDiff(startTimeStr));
|
||
}, interval);
|
||
|
||
return timerId;
|
||
}
|
||
|
||
/**
|
||
* 清除时间差更新定时器
|
||
* @param timerId 定时器ID
|
||
*/
|
||
static clearTimeDiffTimer(timerId: number | null): void {
|
||
if (timerId) {
|
||
clearInterval(timerId);
|
||
}
|
||
}
|
||
} |