import { _decorator} from 'cc'; const { ccclass, property } = _decorator; export enum DateFormat { Y = 'year',// 年 "2025" YM = 'year-month', //年月 "2025-03" YMD = 'year-month-day',// 年月日 "2025-03-01" YMDH = 'year-month-day-hour', // 年月日时 "2025-03-01 14" YMDHM = 'year-month-day-hour-minute',// 年月日时分 "2025-03-01 14:30" YMDHMS = 'year-month-day-hour-minute-second' //年月日时分秒 "2025-03-01 14:30:45" } //日期时间 @ccclass("DateTime") export class DateTime{ /**调用实例 DateTime.formatDate(new Date(),DateFormat.YMD); * 格式化日期字符串 * @param date 传入的日期 * @param format 格式化方式 * @param useUTC 是否使用全球标准时间如何true 则会返回据具有时差的格式化字符串 */ public static formatDate(date: Date | string,format: DateFormat = DateFormat.YMD,useUTC: boolean = false): string { //统一转换为Date对象 const parsedDate = typeof date === 'string' ? new Date(date) : date; if (isNaN(parsedDate.getTime())) { throw new Error('Invalid date input'); } //提取各时间部分(使用本地时间) const data:any = { year: useUTC ? parsedDate.getUTCFullYear() : parsedDate.getFullYear(), month: String((useUTC ? parsedDate.getUTCMonth() : parsedDate.getMonth()) + 1).padStart(2, '0'), day: String(useUTC ? parsedDate.getUTCDate() : parsedDate.getDate()).padStart(2, '0'), hours: String(useUTC ? parsedDate.getUTCHours() : parsedDate.getHours()).padStart(2, '0'), minutes: String(useUTC ? parsedDate.getUTCMinutes() : parsedDate.getMinutes()).padStart(2, '0'), seconds: String(useUTC ? parsedDate.getUTCSeconds() : parsedDate.getSeconds()).padStart(2, '0') }; //根据格式返回对应字符串 switch (format) { case DateFormat.Y: return String(data.year); case DateFormat.YM: return `${data.year}-${data.month}`; case DateFormat.YMD: return `${data.year}-${data.month}-${data.day}`; case DateFormat.YMDH: return `${data.year}-${data.month}-${data.day} ${data.hours}`; case DateFormat.YMDHM: return `${data.year}-${data.month}-${data.day} ${data.hours}:${data.minutes}`; case DateFormat.YMDHMS: return `${data.year}-${data.month}-${data.day} ${data.hours}:${data.minutes}:${data.seconds}`; default: throw new Error('Unsupported format'); } } /** * 获取当前时间的友好显示 (例如: "刚刚", "5分钟前", "2小时前", "昨天", "3天前"等) * @param date 日期 */ public static getFriendlyTime(date: Date | string): string { const now = new Date(); const d = typeof date === 'string' ? new Date(date) : date; const diffInSeconds = Math.floor((now.getTime() - d.getTime()) / 1000); if (diffInSeconds < 60) { return "刚刚"; } else if (diffInSeconds < 3600) { return `${Math.floor(diffInSeconds / 60)}分钟前`; } else if (diffInSeconds < 86400) { return `${Math.floor(diffInSeconds / 3600)}小时前`; } else if (diffInSeconds < 172800) { return "昨天"; } else if (diffInSeconds < 2592000) { return `${Math.floor(diffInSeconds / 86400)}天前`; } else if (diffInSeconds < 31536000) { return `${Math.floor(diffInSeconds / 2592000)}个月前`; } else { return `${Math.floor(diffInSeconds / 31536000)}年前`; } } /** * 判断两个日期是否在同一天 * @param date1 第一个日期 * @param date2 第二个日期 */ public static isSameDay(date1: Date | string, date2: Date | string): boolean { const d1 = typeof date1 === 'string' ? new Date(date1) : date1; const d2 = typeof date2 === 'string' ? new Date(date2) : date2; return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate(); } /** * 获取两个日期之间的天数差 * @param date1 第一个日期 * @param date2 第二个日期 */ public static getDaysBetween(date1: Date | string, date2: Date | string): number { const d1 = typeof date1 === 'string' ? new Date(date1) : date1; const d2 = typeof date2 === 'string' ? new Date(date2) : date2; const diffTime = Math.abs(d2.getTime() - d1.getTime()); return Math.floor(diffTime / (1000 * 60 * 60 * 24)); } /** * 判断传入的日期是否比今天的日期大(属于未来日期) * @param date 可以接受字符串日期(如"2025-04-14")、时间戳(毫秒)或Date对象 * @returns 如果传入的日期比今天大(未来日期),返回 true;否则返回 false */ public static isFutureDate(date: string | number | Date): boolean { let inputDate: Date; //处理不同类型的输入 if(typeof date === 'string') { //字符串日期处理 - 支持多种格式 if (date.includes('T')) {//已经是ISO格式(如"2025-04-14T00:00:00") inputDate = new Date(date); }else{//简单日期格式(如"2025-04-14"),添加时间部分确保UTC处理 inputDate = new Date(date + 'T00:00:00Z'); } }else if(typeof date === 'number') {//时间戳处理 inputDate = new Date(date); } else {//已经是Date对象 inputDate = date; } //验证日期有效性 if(isNaN(inputDate.getTime())) { throw new Error('Invalid date input'); } //获取当前日期(UTC时间,去除时分秒) const currentDate = new Date(); currentDate.setUTCHours(0, 0, 0, 0); //将输入日期也转换为UTC并去除时分秒 const compareDate = new Date(inputDate); compareDate.setUTCHours(0, 0, 0, 0); //比较日期(注意这里应该是 compareDate > currentDate 表示未来日期) return compareDate > currentDate; } }