添加时间处理方法

This commit is contained in:
dgflash
2025-09-16 10:58:58 +08:00
parent a864b3395e
commit bac2c0c710
3 changed files with 32 additions and 8 deletions

View File

@@ -40,9 +40,6 @@ export class DeviceUtil {
/** 是否为字节小游戏 */
static get isByteDance() { return sys.platform === sys.Platform.BYTEDANCE_MINI_GAME; }
/** 是否为百度小游戏 */
static get isBaidu() { return sys.platform === sys.Platform.BAIDU_MINI_GAME; }
/** 是否为 vivo 小游戏 */
static get isVivo() { return sys.platform === sys.Platform.VIVO_MINI_GAME; }

View File

@@ -6,7 +6,7 @@ export class TimeUtil {
* @param time2 结束时间
* @returns
*/
public static daysBetween(time1: number | string | Date, time2: number | string | Date): number {
static daysBetween(time1: number | string | Date, time2: number | string | Date): number {
if (time2 == undefined) {
time2 = +new Date();
}
@@ -18,7 +18,7 @@ export class TimeUtil {
}
/** 间隔秒数,时间顺序无要求,最后会获取绝对值 */
public static secsBetween(time1: number, time2: number) {
static secsBetween(time1: number, time2: number) {
let dates = Math.abs((time2 - time1)) / (1000);
dates = Math.floor(dates) + 1;
return dates;
@@ -28,7 +28,7 @@ export class TimeUtil {
* 代码休眠时间
* @param ms 毫秒
*/
public static async sleep(ms: number) {
static async sleep(ms: number) {
return new Promise<void>((resolve) => {
setTimeout(() => {
resolve();

View File

@@ -1,10 +1,14 @@
declare global {
interface Date {
/** 时间格式化 */
format(format: string): string;
/** 时间加法 */
addTime(addMillis: number): Date;
/** 验证时间是否在指定范围 */
range(t1: number | Date, t2: number | Date): boolean;
}
}
/** 格式化时间字符串 */
Date.prototype.format = function (format: string): string {
const year: number = this.getFullYear();
const month: number = this.getMonth() + 1;
@@ -35,4 +39,27 @@ Date.prototype.format = function (format: string): string {
return r;
};
export { };
Date.prototype.addTime = function (addMillis: number) {
return new Date(this.getTime() + addMillis);
}
Date.prototype.range = function (d1: number | Date, d2: number | Date) {
let t1: number = -1;
let t2: number = -1;
if (d1 instanceof Date)
t1 = d1.getTime();
else
t1 = d1;
if (d2 instanceof Date)
t2 = d2.getTime();
else
t2 = d2;
let now = this.getTime();
if (now >= t1 && now < t2) {
return true;
}
return false;
}
export { };