function pad(value) { return String(value).padStart(2, '0') } export function parseDate(input = new Date()) { if (input instanceof Date) { return new Date(input.getTime()) } if (typeof input === 'number') { return new Date(input) } if (typeof input === 'string') { if (/^\d{4}-\d{2}-\d{2}$/.test(input)) { const [year, month, day] = input.split('-').map(Number) return new Date(year, month - 1, day) } if (/^\d{4}-\d{2}$/.test(input)) { const [year, month] = input.split('-').map(Number) return new Date(year, month - 1, 1) } return new Date(input.replace(/-/g, '/')) } return new Date() } export function toDateKey(input = new Date()) { const date = parseDate(input) return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}` } export function toMonthKey(input = new Date()) { const date = parseDate(input) return `${date.getFullYear()}-${pad(date.getMonth() + 1)}` } export function formatDateLabel(value) { if (!value) { return '' } const date = parseDate(value) const weekMap = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'] return `${date.getMonth() + 1}月${date.getDate()}日 ${weekMap[date.getDay()]}` } export function formatMonthLabel(monthKey) { if (!monthKey) { return '' } const [year, month] = monthKey.split('-') return `${year}年${Number(month)}月` } export function isSameMonth(dateValue, monthKey) { return toMonthKey(dateValue) === monthKey } export function getMonthDays(monthKey) { const [year, month] = monthKey.split('-').map(Number) return new Date(year, month, 0).getDate() } export function getDaysLeftInMonth(monthKey) { const today = new Date() if (toMonthKey(today) !== monthKey) { return getMonthDays(monthKey) } return getMonthDays(monthKey) - today.getDate() + 1 } export function getRecentDateKeys(days, endDate = new Date()) { const result = [] const end = parseDate(endDate) for (let index = days - 1; index >= 0; index -= 1) { const current = new Date(end) current.setDate(end.getDate() - index) result.push(toDateKey(current)) } return result } export function getMonthSeries(length, endMonthKey = toMonthKey()) { const [year, month] = endMonthKey.split('-').map(Number) const cursor = new Date(year, month - 1, 1) const result = [] for (let index = length - 1; index >= 0; index -= 1) { const current = new Date(cursor) current.setMonth(cursor.getMonth() - index) result.push(toMonthKey(current)) } return result }