Files
keeppay/lib/date-utils.ts
T
root 841faca34a feat: 添加项目规范和配置文件
- 新增项目规范文档,包含语言设置和 Git 提交规范
- 更新 .gitignore 文件,添加环境文件和数据目录
- 新增 Prettier 配置文件和忽略文件
- 更新 package.json,添加 prettier 和 prisma 相关依赖
- 新增 API 路由处理佣金和消费数据

这些更改为项目提供了更好的结构和代码风格规范。
2026-05-12 21:43:25 +08:00

80 lines
2.4 KiB
TypeScript

export function getMonthGrid(year: number, month: number): Date[][] {
const firstDay = new Date(year, month, 1)
const startDay = firstDay.getDay()
const startOffset = startDay === 0 ? -6 : 1 - startDay
const startDate = new Date(year, month, 1 + startOffset)
const weeks: Date[][] = []
for (let w = 0; w < 6; w++) {
const week: Date[] = []
for (let d = 0; d < 7; d++) {
const date = new Date(startDate)
date.setDate(startDate.getDate() + w * 7 + d)
week.push(date)
}
weeks.push(week)
}
return weeks
}
export function formatDate(date: Date): string {
const y = date.getFullYear()
const m = String(date.getMonth() + 1).padStart(2, '0')
const d = String(date.getDate()).padStart(2, '0')
return `${y}-${m}-${d}`
}
export function isSameDay(a: Date, b: Date): boolean {
return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate()
}
export function isCurrentMonth(date: Date, year: number, month: number): boolean {
return date.getFullYear() === year && date.getMonth() === month
}
export function isToday(date: Date): boolean {
return isSameDay(date, new Date())
}
export function getMonthName(month: number): string {
const names = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
return names[month]
}
export function getWeekStart(date: Date): Date {
const d = new Date(date)
const day = d.getDay()
const diff = day === 0 ? -6 : 1 - day
d.setDate(d.getDate() + diff)
d.setHours(0, 0, 0, 0)
return d
}
export function getWeekEnd(date: Date): Date {
const d = getWeekStart(date)
d.setDate(d.getDate() + 6)
d.setHours(23, 59, 59, 999)
return d
}
export function getMonthStart(date: Date): Date {
return new Date(date.getFullYear(), date.getMonth(), 1)
}
export function getMonthEnd(date: Date): Date {
return new Date(date.getFullYear(), date.getMonth() + 1, 0, 23, 59, 59, 999)
}
export function getYearStart(date: Date): Date {
return new Date(date.getFullYear(), 0, 1)
}
export function getYearEnd(date: Date): Date {
return new Date(date.getFullYear(), 11, 31, 23, 59, 59, 999)
}
export function isDateInRange(dateStr: string, start: Date, end: Date): boolean {
const d = new Date(dateStr)
return d >= start && d <= end
}