Files
lingji-work-fe/src/utils/DateUtils.ts

431 lines
12 KiB
TypeScript
Raw Normal View History

2025-08-29 14:05:27 +08:00
/**
*
*
*
*
*/
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),
};
}
/**
*
* @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-601
* @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,
};
}
/**
*
* @returns
*/
static getCurrentDateInfo() {
const now = new Date();
now.setHours(0, 0, 0, 0);
const monthRange = this.getMonthRange();
const weekRange = this.getWeekRange();
const yearMonth = this.getCurrentYearMonth();
return {
current: {
date: now,
formatted: this.formatDate(now),
dayOfWeek: this.getChineseDayOfWeek(now),
day: now.getDate(),
month: yearMonth.month,
year: yearMonth.year,
},
month: {
start: monthRange.start,
end: monthRange.end,
startFormatted: monthRange.startFormatted,
endFormatted: monthRange.endFormatted,
totalDays: this.getDaysInMonth(now.getFullYear(), now.getMonth() + 1),
month: yearMonth.month,
year: yearMonth.year,
formatted: this.getFormattedYearMonth(),
chinese: this.getChineseYearMonth(),
},
week: {
start: weekRange.start,
end: weekRange.end,
startFormatted: weekRange.startFormatted,
endFormatted: weekRange.endFormatted,
weekNumber: this.getWeekNumber(now),
},
year: yearMonth.year,
quarter: {
number: this.getCurrentQuarter(),
range: this.getCurrentQuarterRange(),
},
};
}
/**
*
* @param date
* @returns (1-53)
*/
static getWeekNumber(date: Date): number {
const firstDayOfYear = new Date(date.getFullYear(), 0, 1);
const pastDaysOfYear = (date.getTime() - firstDayOfYear.getTime()) / 86400000;
return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
}
/**
*
* @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;