DateTool.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import dayjs from 'dayjs';
  2. const isSameOrBefore = require('dayjs/plugin/isSameOrBefore');
  3. dayjs.extend(isSameOrBefore);
  4. export const DateTool = {
  5. DateFormat: {
  6. YYYY: 'YYYY',
  7. YYYY_MM: 'YYYY-MM',
  8. YYYY_MM_DD: 'YYYY-MM-DD',
  9. YYYY_MM_DD_HH_mm: 'YYYY-MM-DD HH:mm',
  10. YYYY_MM_DD_HH_mm_ss: 'YYYY-MM-DD HH:mm:ss',
  11. YYYY_MM_DD_HH_mm_ss_SSS: 'YYYY-MM-DD HH:mm:ss.SSS',
  12. YYYYMMDD: 'YYYYMMDD',
  13. YYYYMMDDHHmmss: 'YYYYMMDDHHmmss',
  14. YYYY_MM_DD_00_00_00: 'YYYY-MM-DD 00:00:00',
  15. YYYY_MM_DD_23_59_59: 'YYYY-MM-DD 23:59:59',
  16. MM_DD: 'MM-DD',
  17. },
  18. /**
  19. * 获取当天时间刻度
  20. * @param timeSplit 单位分钟
  21. */
  22. getTime: function (timeSplit = 5) {
  23. const now = dayjs();
  24. let zero = dayjs().startOf('date').add(1, 'hour');
  25. const result = [];
  26. while (zero.isSameOrBefore(now)) {
  27. result.push(zero.format('HH:mm'));
  28. zero = zero.add(timeSplit, 'minute');
  29. }
  30. return result;
  31. },
  32. now: (format = DateTool.DateFormat.YYYY_MM_DD) => {
  33. return dayjs().format(format);
  34. },
  35. addDays: function (date, days, format = this.DateFormat.YYYY_MM_DD) {
  36. return dayjs(date).add(days, 'day').format(format);
  37. },
  38. /**
  39. * 获取一段时间的日期
  40. * @param start 开始时间
  41. * @param end 结束时间
  42. * @param format
  43. */
  44. getDayOfRange: function (start, end, format = this.DateFormat.YYYY_MM_DD) {
  45. const dates = [];
  46. let current = dayjs(start);
  47. while (current.isSameOrBefore(end)) {
  48. dates.push(current.format(format));
  49. current = current.add(1, 'day');
  50. }
  51. return dates;
  52. },
  53. getMonthOfRange: function (start, end, format = this.DateFormat.YYYY_MM) {
  54. const dates = [];
  55. let current = dayjs(start);
  56. while (current.isSameOrBefore(end)) {
  57. dates.push(current.format(format));
  58. current = current.add(1, 'month');
  59. }
  60. return dates;
  61. },
  62. get24Time: function (timeSplit = 5) {
  63. const now = dayjs();
  64. let zero = dayjs().subtract(1, 'day').startOf('hour').add(1, 'hour');
  65. const result = [];
  66. while (zero.isSameOrBefore(now)) {
  67. result.push(zero.format('MM-DD HH:mm'));
  68. zero = zero.add(timeSplit, 'minute');
  69. }
  70. return result;
  71. },
  72. getMonthsOfYearAgo: () => {
  73. const month = [];
  74. const currentDate = dayjs();
  75. const oneYearAgo = currentDate.subtract(1, 'year');
  76. let currentMonth = oneYearAgo.startOf('month');
  77. while (currentMonth.isBefore(currentDate.endOf('month'))) {
  78. month.push(currentMonth.format(DateTool.DateFormat.YYYY_MM));
  79. currentMonth = currentMonth.add(1, 'month').startOf('month');
  80. }
  81. return month;
  82. },
  83. timeIndex: (date = dayjs()) => {
  84. return date.format('HH');
  85. },
  86. lastDay: (format = DateTool.DateFormat.YYYY_MM_DD) => {
  87. return dayjs().subtract(1, 'day').format(format);
  88. },
  89. lastMonth: (format = DateTool.DateFormat.YYYY_MM) => {
  90. return dayjs().subtract(1, 'month').format(format);
  91. },
  92. thisMonthLastDay: (format = DateTool.DateFormat.YYYY_MM_DD) => {
  93. return dayjs().endOf('month').format(format);
  94. },
  95. lastYear: (format = DateTool.DateFormat.YYYY) => {
  96. return dayjs().subtract(1, 'year').format(format);
  97. },
  98. lastYearMonthStart: () => {
  99. return dayjs().subtract(1, 'year').format(`${DateTool.DateFormat.YYYY_MM}-01`);
  100. },
  101. thisYear: (format = DateTool.DateFormat.YYYY) => {
  102. return dayjs().format(format);
  103. },
  104. thisMonth: (format = DateTool.DateFormat.YYYY_MM) => {
  105. return dayjs().format(format);
  106. },
  107. };