add FormatText.ts

This commit is contained in:
Timi
2026-04-10 12:42:53 +08:00
parent 5472d00154
commit 4ae8904697
4 changed files with 99 additions and 45 deletions
+17
View File
@@ -1,3 +1,5 @@
import Toolkit from "./Toolkit";
export default class Time {
/** 1 秒时间戳 */
@@ -104,6 +106,21 @@ export default class Time {
return { l, y, d, h, m, s, ms };
}
public static duration(begin?: number, ms?: boolean): string {
if (!begin) {
return "";
}
const r = Time.between(new Date(begin), new Date());
const parts: string[] = [];
Toolkit.doWhere(0 < r.y, () => parts.push(`${r.y}`));
Toolkit.doWhere(0 < r.d, () => parts.push(`${r.d}`));
Toolkit.doWhere(0 < r.h, () => parts.push(`${r.h} 小时`));
Toolkit.doWhere(0 < r.m, () => parts.push(`${r.m} 分钟`));
Toolkit.doWhere(0 < r.s, () => parts.push(`${r.s}`));
Toolkit.doWhere(!!ms && 0 < r.ms, () => parts.push(`${r.ms} 毫秒`));
return parts.join(" ");
}
public static toMediaTime(seconds: number): string {
seconds = Math.floor(seconds);
const hours = Math.floor(seconds / 3600);