|
@@ -3,107 +3,110 @@ import dayjs from 'dayjs';
|
|
|
const isSameOrBefore = require('dayjs/plugin/isSameOrBefore');
|
|
|
dayjs.extend(isSameOrBefore);
|
|
|
export const DateTool = {
|
|
|
- DateFormat: {
|
|
|
- YYYY: 'YYYY',
|
|
|
- YYYY_MM: 'YYYY-MM',
|
|
|
- YYYY_MM_DD: 'YYYY-MM-DD',
|
|
|
- YYYY_MM_DD_HH_mm: 'YYYY-MM-DD HH:mm',
|
|
|
- YYYY_MM_DD_HH_mm_ss: 'YYYY-MM-DD HH:mm:ss',
|
|
|
- YYYY_MM_DD_HH_mm_ss_SSS: 'YYYY-MM-DD HH:mm:ss.SSS',
|
|
|
- YYYYMMDD: 'YYYYMMDD',
|
|
|
- YYYYMMDDHHmmss: 'YYYYMMDDHHmmss',
|
|
|
- YYYY_MM_DD_00_00_00: 'YYYY-MM-DD 00:00:00',
|
|
|
- YYYY_MM_DD_23_59_59: 'YYYY-MM-DD 23:59:59',
|
|
|
- MM_DD: 'MM-DD',
|
|
|
- },
|
|
|
- /**
|
|
|
- * 获取当天时间刻度
|
|
|
- * @param timeSplit 单位分钟
|
|
|
- */
|
|
|
- getTime: function (timeSplit = 5) {
|
|
|
- const now = dayjs();
|
|
|
- let zero = dayjs().startOf('date').add(1, 'hour');
|
|
|
- const result = [];
|
|
|
- while (zero.isSameOrBefore(now)) {
|
|
|
- result.push(zero.format('HH:mm'));
|
|
|
- zero = zero.add(timeSplit, 'minute');
|
|
|
- }
|
|
|
- return result;
|
|
|
- },
|
|
|
- now: (format = DateTool.DateFormat.YYYY_MM_DD) => {
|
|
|
- return dayjs().format(format);
|
|
|
- },
|
|
|
- addDays: function (date, days, format = this.DateFormat.YYYY_MM_DD) {
|
|
|
- return dayjs(date).add(days, 'day').format(format);
|
|
|
- },
|
|
|
- /**
|
|
|
- * 获取一段时间的日期
|
|
|
- * @param start 开始时间
|
|
|
- * @param end 结束时间
|
|
|
- * @param format
|
|
|
- */
|
|
|
- getDayOfRange: function (start, end, format = this.DateFormat.YYYY_MM_DD) {
|
|
|
- const dates = [];
|
|
|
- let current = dayjs(start);
|
|
|
- while (current.isSameOrBefore(end)) {
|
|
|
- dates.push(current.format(format));
|
|
|
- current = current.add(1, 'day');
|
|
|
- }
|
|
|
- return dates;
|
|
|
- },
|
|
|
+ DateFormat: {
|
|
|
+ YYYY: 'YYYY',
|
|
|
+ YYYY_MM: 'YYYY-MM',
|
|
|
+ YYYY_MM_DD: 'YYYY-MM-DD',
|
|
|
+ YYYY_MM_DD_HH_mm: 'YYYY-MM-DD HH:mm',
|
|
|
+ YYYY_MM_DD_HH_mm_ss: 'YYYY-MM-DD HH:mm:ss',
|
|
|
+ YYYY_MM_DD_HH_mm_ss_SSS: 'YYYY-MM-DD HH:mm:ss.SSS',
|
|
|
+ YYYYMMDD: 'YYYYMMDD',
|
|
|
+ YYYYMMDDHHmmss: 'YYYYMMDDHHmmss',
|
|
|
+ YYYY_MM_DD_00_00_00: 'YYYY-MM-DD 00:00:00',
|
|
|
+ YYYY_MM_DD_23_59_59: 'YYYY-MM-DD 23:59:59',
|
|
|
+ MM_DD: 'MM-DD',
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 获取当天时间刻度
|
|
|
+ * @param timeSplit 单位分钟
|
|
|
+ */
|
|
|
+ getTime: function (timeSplit = 5) {
|
|
|
+ const now = dayjs();
|
|
|
+ let zero = dayjs().startOf('date').add(1, 'hour');
|
|
|
+ const result = [];
|
|
|
+ while (zero.isSameOrBefore(now)) {
|
|
|
+ result.push(zero.format('HH:mm'));
|
|
|
+ zero = zero.add(timeSplit, 'minute');
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ },
|
|
|
+ now: (format = DateTool.DateFormat.YYYY_MM_DD) => {
|
|
|
+ return dayjs().format(format);
|
|
|
+ },
|
|
|
+ addDays: function (date, days, format = this.DateFormat.YYYY_MM_DD) {
|
|
|
+ return dayjs(date).add(days, 'day').format(format);
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 获取一段时间的日期
|
|
|
+ * @param start 开始时间
|
|
|
+ * @param end 结束时间
|
|
|
+ * @param format
|
|
|
+ */
|
|
|
+ getDayOfRange: function (start, end, format = this.DateFormat.YYYY_MM_DD) {
|
|
|
+ const dates = [];
|
|
|
+ let current = dayjs(start);
|
|
|
+ while (current.isSameOrBefore(end)) {
|
|
|
+ dates.push(current.format(format));
|
|
|
+ current = current.add(1, 'day');
|
|
|
+ }
|
|
|
+ return dates;
|
|
|
+ },
|
|
|
|
|
|
- getMonthOfRange: function (start, end, format = this.DateFormat.YYYY_MM) {
|
|
|
- const dates = [];
|
|
|
- let current = dayjs(start);
|
|
|
- while (current.isSameOrBefore(end)) {
|
|
|
- dates.push(current.format(format));
|
|
|
- current = current.add(1, 'month');
|
|
|
- }
|
|
|
- return dates;
|
|
|
- },
|
|
|
- get24Time: function (timeSplit = 5) {
|
|
|
- const now = dayjs();
|
|
|
- let zero = dayjs().subtract(1, 'day').startOf('hour').add(1, 'hour');
|
|
|
- const result = [];
|
|
|
- while (zero.isSameOrBefore(now)) {
|
|
|
- result.push(zero.format('MM-DD HH:mm'));
|
|
|
- zero = zero.add(timeSplit, 'minute');
|
|
|
- }
|
|
|
- return result;
|
|
|
- },
|
|
|
- getMonthsOfYearAgo: () => {
|
|
|
- const month = [];
|
|
|
- const currentDate = dayjs();
|
|
|
- const oneYearAgo = currentDate.subtract(1, 'year');
|
|
|
- let currentMonth = oneYearAgo.startOf('month');
|
|
|
- while (currentMonth.isBefore(currentDate.endOf('month'))) {
|
|
|
- month.push(currentMonth.format(DateTool.DateFormat.YYYY_MM));
|
|
|
- currentMonth = currentMonth.add(1, 'month').startOf('month');
|
|
|
- }
|
|
|
- return month;
|
|
|
- },
|
|
|
- timeIndex: (date = dayjs()) => {
|
|
|
- return date.format('HH');
|
|
|
- },
|
|
|
- lastDay: (format = DateTool.DateFormat.YYYY_MM_DD) => {
|
|
|
- return dayjs().subtract(1, 'day').format(format);
|
|
|
- },
|
|
|
- lastMonth: (format = DateTool.DateFormat.YYYY_MM) => {
|
|
|
- return dayjs().subtract(1, 'month').format(format);
|
|
|
- },
|
|
|
- thisMonthLastDay: (format = DateTool.DateFormat.YYYY_MM_DD) => {
|
|
|
- return dayjs().endOf('month').format(format);
|
|
|
- },
|
|
|
- lastYear: (format = DateTool.DateFormat.YYYY) => {
|
|
|
- return dayjs().subtract(1, 'year').format(format);
|
|
|
- },
|
|
|
- lastYearMonthStart: () => {
|
|
|
- return dayjs().subtract(1, 'year').format(`${DateTool.DateFormat.YYYY_MM}-01`);
|
|
|
- },
|
|
|
- thisYear: (format = DateTool.DateFormat.YYYY) => {
|
|
|
- return dayjs().format(format);
|
|
|
- },
|
|
|
- thisMonth: (format = DateTool.DateFormat.YYYY_MM) => {
|
|
|
- return dayjs().format(format);
|
|
|
- },
|
|
|
+ getMonthOfRange: function (start, end, format = this.DateFormat.YYYY_MM) {
|
|
|
+ const dates = [];
|
|
|
+ let current = dayjs(start);
|
|
|
+ while (current.isSameOrBefore(end)) {
|
|
|
+ dates.push(current.format(format));
|
|
|
+ current = current.add(1, 'month');
|
|
|
+ }
|
|
|
+ return dates;
|
|
|
+ },
|
|
|
+ get24Time: function (timeSplit = 5) {
|
|
|
+ const now = dayjs();
|
|
|
+ let zero = dayjs().subtract(1, 'day').startOf('hour').add(1, 'hour');
|
|
|
+ const result = [];
|
|
|
+ while (zero.isSameOrBefore(now)) {
|
|
|
+ result.push(zero.format('MM-DD HH:mm'));
|
|
|
+ zero = zero.add(timeSplit, 'minute');
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ },
|
|
|
+ getMonthsOfYearAgo: () => {
|
|
|
+ const month = [];
|
|
|
+ const currentDate = dayjs();
|
|
|
+ const oneYearAgo = currentDate.subtract(1, 'year');
|
|
|
+ let currentMonth = oneYearAgo.startOf('month');
|
|
|
+ while (currentMonth.isBefore(currentDate.endOf('month'))) {
|
|
|
+ month.push(currentMonth.format(DateTool.DateFormat.YYYY_MM));
|
|
|
+ currentMonth = currentMonth.add(1, 'month').startOf('month');
|
|
|
+ }
|
|
|
+ return month;
|
|
|
+ },
|
|
|
+ timeIndex: (date = dayjs()) => {
|
|
|
+ return date.format('HH');
|
|
|
+ },
|
|
|
+ lastDay: (format = DateTool.DateFormat.YYYY_MM_DD) => {
|
|
|
+ return dayjs().subtract(1, 'day').format(format);
|
|
|
+ },
|
|
|
+ lastMonth: (format = DateTool.DateFormat.YYYY_MM) => {
|
|
|
+ return dayjs().subtract(1, 'month').format(format);
|
|
|
+ },
|
|
|
+ thisMonthLastDay: (format = DateTool.DateFormat.YYYY_MM_DD) => {
|
|
|
+ return dayjs().endOf('month').format(format);
|
|
|
+ },
|
|
|
+ lastYear: (format = DateTool.DateFormat.YYYY) => {
|
|
|
+ return dayjs().subtract(1, 'year').format(format);
|
|
|
+ },
|
|
|
+ lastYearMonthStart: () => {
|
|
|
+ return dayjs().subtract(1, 'year').format(`${DateTool.DateFormat.YYYY_MM}-01`);
|
|
|
+ },
|
|
|
+ thisYear: (format = DateTool.DateFormat.YYYY) => {
|
|
|
+ return dayjs().format(format);
|
|
|
+ },
|
|
|
+ thisMonth: (format = DateTool.DateFormat.YYYY_MM) => {
|
|
|
+ return dayjs().format(format);
|
|
|
+ },
|
|
|
+ nearYearStartDay: (format = DateTool.DateFormat.YYYY_MM_DD) => {
|
|
|
+ return dayjs().subtract(1, 'year').startOf('month').format(format);
|
|
|
+ },
|
|
|
};
|