1836b7db2c8a72e8bf7572482f286fb20f8e563e.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, _dec, _class, _crd, ccclass, property, DateFormat, DateTime;
  4. return {
  5. setters: [function (_cc) {
  6. _cclegacy = _cc.cclegacy;
  7. __checkObsolete__ = _cc.__checkObsolete__;
  8. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  9. _decorator = _cc._decorator;
  10. }],
  11. execute: function () {
  12. _crd = true;
  13. _cclegacy._RF.push({}, "8cc67MqISNOA5U7a2gprve+", "DateTime", undefined);
  14. __checkObsolete__(['_decorator']);
  15. ({
  16. ccclass,
  17. property
  18. } = _decorator);
  19. _export("DateFormat", DateFormat = /*#__PURE__*/function (DateFormat) {
  20. DateFormat["Y"] = "year";
  21. DateFormat["YM"] = "year-month";
  22. DateFormat["YMD"] = "year-month-day";
  23. DateFormat["YMDH"] = "year-month-day-hour";
  24. DateFormat["YMDHM"] = "year-month-day-hour-minute";
  25. DateFormat["YMDHMS"] = "year-month-day-hour-minute-second";
  26. return DateFormat;
  27. }({})); //日期时间
  28. _export("DateTime", DateTime = (_dec = ccclass("DateTime"), _dec(_class = class DateTime {
  29. /**调用实例 DateTime.formatDate(new Date(),DateFormat.YMD);
  30. * 格式化日期字符串
  31. * @param date 传入的日期
  32. * @param format 格式化方式
  33. * @param useUTC 是否使用全球标准时间如何true 则会返回据具有时差的格式化字符串
  34. */
  35. static formatDate(date, format, useUTC) {
  36. if (format === void 0) {
  37. format = DateFormat.YMD;
  38. }
  39. if (useUTC === void 0) {
  40. useUTC = false;
  41. }
  42. //统一转换为Date对象
  43. var parsedDate = typeof date === 'string' ? new Date(date) : date;
  44. if (isNaN(parsedDate.getTime())) {
  45. throw new Error('Invalid date input');
  46. } //提取各时间部分(使用本地时间)
  47. var data = {
  48. year: useUTC ? parsedDate.getUTCFullYear() : parsedDate.getFullYear(),
  49. month: String((useUTC ? parsedDate.getUTCMonth() : parsedDate.getMonth()) + 1).padStart(2, '0'),
  50. day: String(useUTC ? parsedDate.getUTCDate() : parsedDate.getDate()).padStart(2, '0'),
  51. hours: String(useUTC ? parsedDate.getUTCHours() : parsedDate.getHours()).padStart(2, '0'),
  52. minutes: String(useUTC ? parsedDate.getUTCMinutes() : parsedDate.getMinutes()).padStart(2, '0'),
  53. seconds: String(useUTC ? parsedDate.getUTCSeconds() : parsedDate.getSeconds()).padStart(2, '0')
  54. }; //根据格式返回对应字符串
  55. switch (format) {
  56. case DateFormat.Y:
  57. return String(data.year);
  58. case DateFormat.YM:
  59. return data.year + "-" + data.month;
  60. case DateFormat.YMD:
  61. return data.year + "-" + data.month + "-" + data.day;
  62. case DateFormat.YMDH:
  63. return data.year + "-" + data.month + "-" + data.day + " " + data.hours;
  64. case DateFormat.YMDHM:
  65. return data.year + "-" + data.month + "-" + data.day + " " + data.hours + ":" + data.minutes;
  66. case DateFormat.YMDHMS:
  67. return data.year + "-" + data.month + "-" + data.day + " " + data.hours + ":" + data.minutes + ":" + data.seconds;
  68. default:
  69. throw new Error('Unsupported format');
  70. }
  71. }
  72. /**
  73. * 获取当前时间的友好显示 (例如: "刚刚", "5分钟前", "2小时前", "昨天", "3天前"等)
  74. * @param date 日期
  75. */
  76. static getFriendlyTime(date) {
  77. var now = new Date();
  78. var d = typeof date === 'string' ? new Date(date) : date;
  79. var diffInSeconds = Math.floor((now.getTime() - d.getTime()) / 1000);
  80. if (diffInSeconds < 60) {
  81. return "刚刚";
  82. } else if (diffInSeconds < 3600) {
  83. return Math.floor(diffInSeconds / 60) + "\u5206\u949F\u524D";
  84. } else if (diffInSeconds < 86400) {
  85. return Math.floor(diffInSeconds / 3600) + "\u5C0F\u65F6\u524D";
  86. } else if (diffInSeconds < 172800) {
  87. return "昨天";
  88. } else if (diffInSeconds < 2592000) {
  89. return Math.floor(diffInSeconds / 86400) + "\u5929\u524D";
  90. } else if (diffInSeconds < 31536000) {
  91. return Math.floor(diffInSeconds / 2592000) + "\u4E2A\u6708\u524D";
  92. } else {
  93. return Math.floor(diffInSeconds / 31536000) + "\u5E74\u524D";
  94. }
  95. }
  96. /**
  97. * 判断两个日期是否在同一天
  98. * @param date1 第一个日期
  99. * @param date2 第二个日期
  100. */
  101. static isSameDay(date1, date2) {
  102. var d1 = typeof date1 === 'string' ? new Date(date1) : date1;
  103. var d2 = typeof date2 === 'string' ? new Date(date2) : date2;
  104. return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate();
  105. }
  106. /**
  107. * 获取两个日期之间的天数差
  108. * @param date1 第一个日期
  109. * @param date2 第二个日期
  110. */
  111. static getDaysBetween(date1, date2) {
  112. var d1 = typeof date1 === 'string' ? new Date(date1) : date1;
  113. var d2 = typeof date2 === 'string' ? new Date(date2) : date2;
  114. var diffTime = Math.abs(d2.getTime() - d1.getTime());
  115. return Math.floor(diffTime / (1000 * 60 * 60 * 24));
  116. }
  117. /**
  118. * 判断传入的日期是否比今天的日期大(属于未来日期)
  119. * @param date 可以接受字符串日期(如"2025-04-14")、时间戳(毫秒)或Date对象
  120. * @returns 如果传入的日期比今天大(未来日期),返回 true;否则返回 false
  121. */
  122. static isFutureDate(date) {
  123. var inputDate; //处理不同类型的输入
  124. if (typeof date === 'string') {
  125. //字符串日期处理 - 支持多种格式
  126. if (date.includes('T')) {
  127. //已经是ISO格式(如"2025-04-14T00:00:00")
  128. inputDate = new Date(date);
  129. } else {
  130. //简单日期格式(如"2025-04-14"),添加时间部分确保UTC处理
  131. inputDate = new Date(date + 'T00:00:00Z');
  132. }
  133. } else if (typeof date === 'number') {
  134. //时间戳处理
  135. inputDate = new Date(date);
  136. } else {
  137. //已经是Date对象
  138. inputDate = date;
  139. } //验证日期有效性
  140. if (isNaN(inputDate.getTime())) {
  141. throw new Error('Invalid date input');
  142. } //获取当前日期(UTC时间,去除时分秒)
  143. var currentDate = new Date();
  144. currentDate.setUTCHours(0, 0, 0, 0); //将输入日期也转换为UTC并去除时分秒
  145. var compareDate = new Date(inputDate);
  146. compareDate.setUTCHours(0, 0, 0, 0); //比较日期(注意这里应该是 compareDate > currentDate 表示未来日期)
  147. return compareDate > currentDate;
  148. }
  149. }) || _class));
  150. _cclegacy._RF.pop();
  151. _crd = false;
  152. }
  153. };
  154. });
  155. //# sourceMappingURL=1836b7db2c8a72e8bf7572482f286fb20f8e563e.js.map