50 lines
1.4 KiB
TypeScript
50 lines
1.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]
|
|
}
|