export enum DateFormat { CHINA = '年月日', UNDERLINE = '-', FULL_STOP = '.', } export class YTDateUtil { private static timestampCache = new Map(); // 格式化日期 xx月xx日 static formatMonth(date: Date): string { return `${YTDateUtil.padNumber(date.getMonth() + 1)}月${YTDateUtil.padNumber(date.getDate())}日` } // 格式化日期 yyyy年xx月xx日 static formatDate(date: Date, format: DateFormat = DateFormat.CHINA): string { if (format == DateFormat.CHINA) { return `${date.getFullYear()}年${YTDateUtil.padNumber(date.getMonth() + 1)}月${YTDateUtil.padNumber(date.getDate())}日` } return `${date.getFullYear()}${format}${YTDateUtil.padNumber(date.getMonth() + 1)}${format}${YTDateUtil.padNumber(date.getDate())}` } /* static formatDate(date: Date): string { return `${date.getFullYear()}年${date.getMonth() + 1}月` }*/ // 组合格式化(如:周二 00:00:00) static formatFull(date: Date): string { return `${YTDateUtil.getWeekCN(date)} ${date.toTimeString().slice(0, 8)}` } // 获取中文星期几 static getWeekCN(date: Date): string { const week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'] return week[date.getDay()] } // 补零工具方法 static padNumber(num: number): string { return num.toString().padStart(2, '0'); } static SATURDAY = 6; // 日历表上周六对应的序列号,从周日开始算起,取值0~6 /* * 根据指定年份和月份获取该月在日历表上的日期排布数据 * @param {number} specifiedMonth - 指定月份 * @param {number} specifiedYear - 指定年份 */ static getMonthDate(specifiedMonth: number, specifiedYear: number) { let currentFirstWeekDay: number = 0; // 初始化指定月的第一天是周几 let currentLastWeekDay: number = 0; // 初始化指定月的最后一天是周几 let currentAllDay: number[] = []; // 初始化指定月的日期排列数组 let totalDays = new Date(specifiedYear, specifiedMonth, 0).getDate(); // 初始化指定月总天数 currentFirstWeekDay = new Date(specifiedYear, specifiedMonth - 1, 1).getDay(); // 获取指定月的第一天是周几 currentLastWeekDay = new Date(specifiedYear, specifiedMonth - 1, totalDays).getDay(); // 获取指定月的最后一天是周几 // 将月份中显示上个月日期的内容置0 for (let item = 0; item < currentFirstWeekDay; item++) { currentAllDay[item] = 0; } // 将本月日期内容存入数组 for (let item = 1; item <= totalDays; item++) { currentAllDay.push(item); } // 将月份中显示下个月日期的内容置0 for (let item = 0; item < YTDateUtil.SATURDAY - currentLastWeekDay; item++) { currentAllDay.push(0); } return currentAllDay; } // 深拷贝日期对象 并且 将日期对象中的时、分、秒、毫秒归零 public static normalizeDate(date: Date): Date { return new Date(date.getFullYear(), date.getMonth(), date.getDate()); } // 在是否在日期范围内 public static isDateBetween(target: Date, start: Date, end: Date): boolean { const time = target.getTime(); return time > start.getTime() && time < end.getTime(); } public static isDateBetweenByTimestamp(target: Date, startTime: number, endTime: number): boolean { const targetTime = target.getTime(); return targetTime > startTime && targetTime < endTime; } static getCachedTime(date: Date): number { const key = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`; if (!YTDateUtil.timestampCache.has(key)) { YTDateUtil.timestampCache.set(key, date.getTime()); } return YTDateUtil.timestampCache.get(key)!; } //计算两个日期的天数差 static getDaysBetweenDates(startDate: Date, endDate: Date): number { const startTime = startDate.getTime(); const endTime = endDate.getTime(); const diffTime = Math.abs(endTime - startTime); const diffDays =Math.floor(diffTime / (1000 * 3600 * 24)) + 1; return diffDays; } //获取多少年的今天 static getYearToday(year: number): Date { const today = new Date(); const yearToday = new Date(today.getFullYear() + year, today.getMonth(), today.getDate()); return yearToday; } }