add Time.toString

This commit is contained in:
Timi
2026-01-05 15:11:07 +08:00
parent d74ddf4ba2
commit 207e242696

View File

@ -69,6 +69,26 @@ export default class Time {
}
}
/**
* 将毫秒值转换为可读的时间字符串
*
* @param ms 毫秒值
* @return 时间字符串,如 "10 天"、"5 小时"、"30 分钟"、"15 秒"、"500 毫秒"
*/
public static toString(ms: number): string {
if (Time.D <= ms) {
return `${Math.floor(ms / Time.D)}`;
} else if (Time.H <= ms) {
return `${Math.floor(ms / Time.H)} 小时`;
} else if (Time.M <= ms) {
return `${Math.floor(ms / Time.M)} 分钟`;
} else if (Time.S <= ms) {
return `${Math.floor(ms / Time.S)}`;
} else {
return `${Math.floor(ms)} 毫秒`;
}
}
public static between(begin: Date, end?: Date) : any {
if (!end) {
end = new Date();