123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- System.register(["cc"], function (_export, _context) {
- "use strict";
- var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, _dec, _class, _crd, ccclass, property, DateFormat, DateTime;
- return {
- setters: [function (_cc) {
- _cclegacy = _cc.cclegacy;
- __checkObsolete__ = _cc.__checkObsolete__;
- __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
- _decorator = _cc._decorator;
- }],
- execute: function () {
- _crd = true;
- _cclegacy._RF.push({}, "8cc67MqISNOA5U7a2gprve+", "DateTime", undefined);
- __checkObsolete__(['_decorator']);
- ({
- ccclass,
- property
- } = _decorator);
- _export("DateFormat", DateFormat = /*#__PURE__*/function (DateFormat) {
- DateFormat["Y"] = "year";
- DateFormat["YM"] = "year-month";
- DateFormat["YMD"] = "year-month-day";
- DateFormat["YMDH"] = "year-month-day-hour";
- DateFormat["YMDHM"] = "year-month-day-hour-minute";
- DateFormat["YMDHMS"] = "year-month-day-hour-minute-second";
- return DateFormat;
- }({})); //日期时间
- _export("DateTime", DateTime = (_dec = ccclass("DateTime"), _dec(_class = class DateTime {
- /**调用实例 DateTime.formatDate(new Date(),DateFormat.YMD);
- * 格式化日期字符串
- * @param date 传入的日期
- * @param format 格式化方式
- * @param useUTC 是否使用全球标准时间如何true 则会返回据具有时差的格式化字符串
- */
- static formatDate(date, format, useUTC) {
- if (format === void 0) {
- format = DateFormat.YMD;
- }
- if (useUTC === void 0) {
- useUTC = false;
- }
- //统一转换为Date对象
- var parsedDate = typeof date === 'string' ? new Date(date) : date;
- if (isNaN(parsedDate.getTime())) {
- throw new Error('Invalid date input');
- } //提取各时间部分(使用本地时间)
- var data = {
- 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 日期
- */
- static getFriendlyTime(date) {
- var now = new Date();
- var d = typeof date === 'string' ? new Date(date) : date;
- var diffInSeconds = Math.floor((now.getTime() - d.getTime()) / 1000);
- if (diffInSeconds < 60) {
- return "刚刚";
- } else if (diffInSeconds < 3600) {
- return Math.floor(diffInSeconds / 60) + "\u5206\u949F\u524D";
- } else if (diffInSeconds < 86400) {
- return Math.floor(diffInSeconds / 3600) + "\u5C0F\u65F6\u524D";
- } else if (diffInSeconds < 172800) {
- return "昨天";
- } else if (diffInSeconds < 2592000) {
- return Math.floor(diffInSeconds / 86400) + "\u5929\u524D";
- } else if (diffInSeconds < 31536000) {
- return Math.floor(diffInSeconds / 2592000) + "\u4E2A\u6708\u524D";
- } else {
- return Math.floor(diffInSeconds / 31536000) + "\u5E74\u524D";
- }
- }
- /**
- * 判断两个日期是否在同一天
- * @param date1 第一个日期
- * @param date2 第二个日期
- */
- static isSameDay(date1, date2) {
- var d1 = typeof date1 === 'string' ? new Date(date1) : date1;
- var 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 第二个日期
- */
- static getDaysBetween(date1, date2) {
- var d1 = typeof date1 === 'string' ? new Date(date1) : date1;
- var d2 = typeof date2 === 'string' ? new Date(date2) : date2;
- var diffTime = Math.abs(d2.getTime() - d1.getTime());
- return Math.floor(diffTime / (1000 * 60 * 60 * 24));
- }
- /**
- * 判断传入的日期是否比今天的日期大(属于未来日期)
- * @param date 可以接受字符串日期(如"2025-04-14")、时间戳(毫秒)或Date对象
- * @returns 如果传入的日期比今天大(未来日期),返回 true;否则返回 false
- */
- static isFutureDate(date) {
- var inputDate; //处理不同类型的输入
- 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时间,去除时分秒)
- var currentDate = new Date();
- currentDate.setUTCHours(0, 0, 0, 0); //将输入日期也转换为UTC并去除时分秒
- var compareDate = new Date(inputDate);
- compareDate.setUTCHours(0, 0, 0, 0); //比较日期(注意这里应该是 compareDate > currentDate 表示未来日期)
- return compareDate > currentDate;
- }
- }) || _class));
- _cclegacy._RF.pop();
- _crd = false;
- }
- };
- });
- //# sourceMappingURL=1836b7db2c8a72e8bf7572482f286fb20f8e563e.js.map
|