From 60b62e86ef6adacdc19e565030c20890d2e60186 Mon Sep 17 00:00:00 2001 From: dgflash Date: Thu, 16 Oct 2025 20:52:51 +0800 Subject: [PATCH] =?UTF-8?q?TimeUtil.format=E6=B7=BB=E5=8A=A0=E5=80=92?= =?UTF-8?q?=E8=AE=A1=E6=97=B6=E7=A7=92=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=B8=BA?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/core/utils/TimeUtils.ts | 32 ++++++++++++++++++++++++++++++++ assets/module/common/CCEntity.ts | 5 ++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/assets/core/utils/TimeUtils.ts b/assets/core/utils/TimeUtils.ts index dfd2161..3e486fd 100644 --- a/assets/core/utils/TimeUtils.ts +++ b/assets/core/utils/TimeUtils.ts @@ -35,4 +35,36 @@ export class TimeUtil { }, ms) }); } + + /** 倒计时时秒格式为时间格式 */ + static format(countDown: number) { + let c: number = countDown; + let date: number = Math.floor(c / 86400); + c = c - date * 86400; + let hours: number = Math.floor(c / 3600); + c = c - hours * 3600; + let minutes: number = Math.floor(c / 60); + c = c - minutes * 60; + let seconds: number = c; + + let result: string = ""; + let timeFormat: string = "{0}:{1}:{2}"; + if (date == 0 && hours == 0 && minutes == 0 && seconds == 0) { + result = this.replace(timeFormat, "00", "00", "00"); + } + else { + hours += date * 24; + result = this.replace(timeFormat, this.coverString(hours), this.coverString(minutes), this.coverString(seconds)); // 否则显示 "01:12:24" + } + return result; + } + + private static coverString(value: number) { + if (value < 10) return "0" + value; + return value.toString(); + } + + private static replace(value: string, ...args: any): string { + return value.replace(/\{(\d+)\}/g, function (m, i) { return args[i]; }); + } } \ No newline at end of file diff --git a/assets/module/common/CCEntity.ts b/assets/module/common/CCEntity.ts index 79135f2..41ef720 100644 --- a/assets/module/common/CCEntity.ts +++ b/assets/module/common/CCEntity.ts @@ -190,7 +190,10 @@ export abstract class CCEntity extends ecs.Entity { * @returns 业务逻辑组件实例 */ getBusiness>(cls: any): T { - return this.businesss.get(cls) as T; + if (this.businesss) { + return this.businesss.get(cls) as T; + } + return null!; } /**