DateTime.ts 6.2 KB

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