diff --git a/src/utils/Time.ts b/src/utils/Time.ts index 509ce53..5f0a653 100644 --- a/src/utils/Time.ts +++ b/src/utils/Time.ts @@ -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();