/** * 日期工具类 * 提供月维度和周维度的开始结束日期获取功能 * 周以星期日开始,星期六结束 * 只返回日期部分,不包含时间 */ class DateUtils { /** * 获取当前时间的月开始日期和结束日期 * @returns 包含开始和结束日期的对象 */ static getMonthRange(): { start: Date; end: Date; startFormatted: string; endFormatted: string; } { const now = new Date(); // 月开始日期(当月第一天) const start = new Date(now.getFullYear(), now.getMonth(), 1); start.setHours(0, 0, 0, 0); // 月结束日期(当月最后一天) const end = new Date(now.getFullYear(), now.getMonth() + 1, 0); end.setHours(0, 0, 0, 0); return { start, end, startFormatted: this.formatDate(start), endFormatted: this.formatDate(end), }; } static MonthStrToDate(dateStr: string): Date { const year = parseInt(dateStr.split('年')[0]); const month = parseInt(dateStr.split('年')[1].split('月')[0]) - 1; // 月份从0开始 const date = new Date(year, month, 1); return date; } /** * 获取指定年份和月的范围 * @param year 年份 * @param month 月份(1-12) */ static getMonthRangeByYearMonth( year: number, month: number, ): { start: Date; end: Date; startFormatted: string; endFormatted: string; } { // 月开始日期 const start = new Date(year, month - 1, 1); start.setHours(0, 0, 0, 0); // 月结束日期 const end = new Date(year, month, 0); end.setHours(0, 0, 0, 0); return { start, end, startFormatted: this.formatDate(start), endFormatted: this.formatDate(end), }; } /** * 获取当前时间的周范围(以星期日开始) * @returns 包含开始和结束日期的对象 */ static getWeekRange(): { start: Date; end: Date; startFormatted: string; endFormatted: string; } { const today = new Date(); today.setHours(0, 0, 0, 0); const dayOfWeek = today.getDay(); // 0是周日,1是周一,...,6是周六 // 周开始日期(周日) const start = new Date(today); start.setDate(today.getDate() - dayOfWeek); start.setHours(0, 0, 0, 0); // 周结束日期(周六) const end = new Date(start); end.setDate(start.getDate() + 6); end.setHours(0, 0, 0, 0); return { start, end, startFormatted: this.formatDate(start), endFormatted: this.formatDate(end), }; } /** * 获取指定日期的周范围 * @param date 指定日期 */ static getWeekRangeByDate(date: Date): { start: Date; end: Date; startFormatted: string; endFormatted: string; } { const inputDate = new Date(date); inputDate.setHours(0, 0, 0, 0); const dayOfWeek = inputDate.getDay(); // 周开始日期(周日) const start = new Date(inputDate); start.setDate(inputDate.getDate() - dayOfWeek); start.setHours(0, 0, 0, 0); // 周结束日期(周六) const end = new Date(start); end.setDate(start.getDate() + 6); end.setHours(0, 0, 0, 0); return { start, end, startFormatted: this.formatDate(start), endFormatted: this.formatDate(end), }; } /** * 格式化日期(只返回日期部分) * @param date 日期对象 * @returns 格式化后的日期字符串 (YYYY-MM-DD) */ static formatDate(date: Date): string { const year = date.getFullYear(); const month = (date.getMonth() + 1).toString().padStart(2, '0'); const day = date.getDate().toString().padStart(2, '0'); return `${year}-${month}-${day}`; } /** * 获取当前日期信息 * @returns 包含各种格式的当前日期信息 */ static getCurrentDateInfo() { const now = new Date(); now.setHours(0, 0, 0, 0); const monthRange = this.getMonthRange(); const weekRange = this.getWeekRange(); return { current: { date: now, formatted: this.formatDate(now), dayOfWeek: this.getChineseDayOfWeek(now), }, month: { start: monthRange.start, end: monthRange.end, startFormatted: monthRange.startFormatted, endFormatted: monthRange.endFormatted, totalDays: this.getDaysInMonth(now.getFullYear(), now.getMonth() + 1), }, week: { start: weekRange.start, end: weekRange.end, startFormatted: weekRange.startFormatted, endFormatted: weekRange.endFormatted, }, }; } /** * 获取指定月份的天数 * @param year 年份 * @param month 月份(1-12) * @returns 该月的天数 */ static getDaysInMonth(year: number, month: number): number { return new Date(year, month, 0).getDate(); } /** * 获取中文星期几 * @param date 日期对象 * @returns 中文星期几 */ static getChineseDayOfWeek(date: Date): string { const days = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']; return days[date.getDay()]; } /** * 获取中文月份名称 * @param month 月份(1-12) * @returns 中文月份名称 */ static getChineseMonthName(month: number): string { const months = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']; return months[month - 1] || ''; } /** * 判断是否为同一天 * @param date1 日期1 * @param date2 日期2 * @returns 是否为同一天 */ static isSameDay(date1: Date, date2: Date): boolean { return this.formatDate(date1) === this.formatDate(date2); } /** * 获取日期差(天数) * @param date1 日期1 * @param date2 日期2 * @returns 相差的天数 */ static getDaysDifference(date1: Date, date2: Date): number { const d1 = new Date(date1); const d2 = new Date(date2); d1.setHours(0, 0, 0, 0); d2.setHours(0, 0, 0, 0); const diffTime = Math.abs(d2.getTime() - d1.getTime()); return Math.ceil(diffTime / (1000 * 60 * 60 * 24)); } /** * 获取当前周的所有日期及星期几 * @param startDay 周起始日(0-6,0表示周日,默认1表示周一) * @returns 当前周的日期数组 */ static getWeekDaysByDate(targetDate: Date, startDay: number = 1) { const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']; const currentDay = targetDate.getDay(); const firstDayOffset = (currentDay - startDay + 7) % 7; const firstDayOfWeek = new Date(targetDate); firstDayOfWeek.setDate(targetDate.getDate() - firstDayOffset); return Array.from({ length: 7 }).map((_, i) => { const date = new Date(firstDayOfWeek); date.setDate(firstDayOfWeek.getDate() + i); return { date, day: date.getDate(), weekday: weekdays[date.getDay()] + ' ' + date.getDate(), month: date.getMonth() + 1, // 添加月份信息(1-12) year: date.getFullYear(), }; }); } // 获取某个月每一天的星期几 static getDaysAndWeekdays(year: number, month: number): Array<{ day: number; weekday: string }> { const daysInMonth = new Date(year, month + 1, 0).getDate(); // 获取当月总天数 const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']; // 中文星期数组 const days: Array<{ day: number; weekday: string }> = []; // 结果数组 for (let day = 1; day <= daysInMonth; day++) { const date = new Date(year, month, day); const weekdayIndex = date.getDay(); // 获取星期几的索引(0-6) days.push({ day, weekday: weekdays[weekdayIndex] + ' ' + day, }); } return days; } /** * 获取当前的年份 * @returns 当前年份 */ static getCurrentYear(): number { return new Date().getFullYear(); } /** * 获取当前的月份 * @returns 当前月份 (1-12) */ static getCurrentMonth(): number { return new Date().getMonth() + 1; } /** * 获取当前的年份和月份 * @returns 包含当前年份和月份的对象 */ static getCurrentYearMonth(): { year: number; month: number } { const now = new Date(); return { year: now.getFullYear(), month: now.getMonth() + 1, }; } /** * 获取格式化的当前年月字符串 * @param separator 分隔符,默认为 '-' * @returns 格式化的年月字符串 (YYYY-MM) */ static getFormattedYearMonth(separator: string = '-'): string { const now = new Date(); const year = now.getFullYear(); const month = (now.getMonth() + 1).toString().padStart(2, '0'); return `${year}${separator}${month}`; } /** * 获取中文格式的当前年月 * @returns 中文年月字符串 (YYYY年MM月) */ static getChineseYearMonth(): string { const now = new Date(); const year = now.getFullYear(); const month = (now.getMonth() + 1).toString().padStart(2, '0'); return `${year}年${month}月`; } /** * 获取当前季度 * @returns 当前季度 (1-4) */ static getCurrentQuarter(): number { const month = new Date().getMonth() + 1; return Math.ceil(month / 3); } /** * 获取当前季度的开始和结束月份 * @returns 包含季度开始和结束月份的对象 */ static getCurrentQuarterRange(): { startMonth: number; endMonth: number } { const quarter = this.getCurrentQuarter(); return { startMonth: (quarter - 1) * 3 + 1, endMonth: quarter * 3, }; } /** * 获取指定年份的所有月份信息 * @param year 年份 * @returns 包含所有月份信息的数组 */ static getYearMonths(year: number): Array<{ month: number; name: string; days: number; formatted: string; }> { return Array.from({ length: 12 }, (_, i) => { const month = i + 1; return { month, name: this.getChineseMonthName(month), days: this.getDaysInMonth(year, month), formatted: `${year}-${month.toString().padStart(2, '0')}`, }; }); } static formatDateToWeekdayDay(date: Date): string { const day = date.getDate(); const weekday = this.getChineseDayOfWeek(date).replace('星期', '周'); return `${weekday} ${day}`; } } export default DateUtils;