YTDateUtil.ets 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. export enum DateFormat {
  2. CHINA = '年月日',
  3. UNDERLINE = '-',
  4. FULL_STOP = '.',
  5. }
  6. export class YTDateUtil {
  7. private static timestampCache = new Map<string, number>();
  8. // 格式化日期 xx月xx日
  9. static formatMonth(date: Date): string {
  10. return `${YTDateUtil.padNumber(date.getMonth() + 1)}月${YTDateUtil.padNumber(date.getDate())}日`
  11. }
  12. // 格式化日期 yyyy年xx月xx日
  13. static formatDate(date: Date, format: DateFormat = DateFormat.CHINA): string {
  14. if (format == DateFormat.CHINA) {
  15. return `${date.getFullYear()}年${YTDateUtil.padNumber(date.getMonth() + 1)}月${YTDateUtil.padNumber(date.getDate())}日`
  16. }
  17. return `${date.getFullYear()}${format}${YTDateUtil.padNumber(date.getMonth() + 1)}${format}${YTDateUtil.padNumber(date.getDate())}`
  18. }
  19. /* static formatDate(date: Date): string {
  20. return `${date.getFullYear()}年${date.getMonth() + 1}月`
  21. }*/
  22. // 组合格式化(如:周二 00:00:00)
  23. static formatFull(date: Date): string {
  24. return `${YTDateUtil.getWeekCN(date)} ${date.toTimeString().slice(0, 8)}`
  25. }
  26. // 获取中文星期几
  27. static getWeekCN(date: Date): string {
  28. const week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  29. return week[date.getDay()]
  30. }
  31. // 补零工具方法
  32. static padNumber(num: number): string {
  33. return num.toString().padStart(2, '0');
  34. }
  35. static SATURDAY = 6; // 日历表上周六对应的序列号,从周日开始算起,取值0~6
  36. /*
  37. * 根据指定年份和月份获取该月在日历表上的日期排布数据
  38. * @param {number} specifiedMonth - 指定月份
  39. * @param {number} specifiedYear - 指定年份
  40. */
  41. static getMonthDate(specifiedMonth: number, specifiedYear: number) {
  42. let currentFirstWeekDay: number = 0; // 初始化指定月的第一天是周几
  43. let currentLastWeekDay: number = 0; // 初始化指定月的最后一天是周几
  44. let currentAllDay: number[] = []; // 初始化指定月的日期排列数组
  45. let totalDays = new Date(specifiedYear, specifiedMonth, 0).getDate(); // 初始化指定月总天数
  46. currentFirstWeekDay = new Date(specifiedYear, specifiedMonth - 1, 1).getDay(); // 获取指定月的第一天是周几
  47. currentLastWeekDay = new Date(specifiedYear, specifiedMonth - 1, totalDays).getDay(); // 获取指定月的最后一天是周几
  48. // 将月份中显示上个月日期的内容置0
  49. for (let item = 0; item < currentFirstWeekDay; item++) {
  50. currentAllDay[item] = 0;
  51. }
  52. // 将本月日期内容存入数组
  53. for (let item = 1; item <= totalDays; item++) {
  54. currentAllDay.push(item);
  55. }
  56. // 将月份中显示下个月日期的内容置0
  57. for (let item = 0; item < YTDateUtil.SATURDAY - currentLastWeekDay; item++) {
  58. currentAllDay.push(0);
  59. }
  60. return currentAllDay;
  61. }
  62. // 深拷贝日期对象 并且 将日期对象中的时、分、秒、毫秒归零
  63. public static normalizeDate(date: Date): Date {
  64. return new Date(date.getFullYear(), date.getMonth(), date.getDate());
  65. }
  66. // 在是否在日期范围内
  67. public static isDateBetween(target: Date, start: Date, end: Date): boolean {
  68. const time = target.getTime();
  69. return time > start.getTime() && time < end.getTime();
  70. }
  71. public static isDateBetweenByTimestamp(target: Date, startTime: number, endTime: number): boolean {
  72. const targetTime = target.getTime();
  73. return targetTime > startTime && targetTime < endTime;
  74. }
  75. static getCachedTime(date: Date): number {
  76. const key = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
  77. if (!YTDateUtil.timestampCache.has(key)) {
  78. YTDateUtil.timestampCache.set(key, date.getTime());
  79. }
  80. return YTDateUtil.timestampCache.get(key)!;
  81. }
  82. //计算两个日期的天数差
  83. static getDaysBetweenDates(startDate: Date, endDate: Date): number {
  84. const startTime = startDate.getTime();
  85. const endTime = endDate.getTime();
  86. const diffTime = Math.abs(endTime - startTime);
  87. const diffDays =Math.floor(diffTime / (1000 * 3600 * 24)) + 1;
  88. return diffDays;
  89. }
  90. //获取多少年的今天
  91. static getYearToday(year: number): Date {
  92. const today = new Date();
  93. const yearToday = new Date(today.getFullYear() + year, today.getMonth(), today.getDate());
  94. return yearToday;
  95. }
  96. }