export function formatAmount(fen: number): string { const yuan = (fen / 100).toFixed(2) return '¥ ' + yuan.replace(/\B(?=(\d{3})+(?!\d))/g, ',') } export function formatDate(date: string | Date): string { if (!date) return '' // 处理 Date 对象或 ISO 字符串 let d: Date if (date instanceof Date) { d = date } else if (typeof date === 'string') { // 处理 YYYY-MM-DD 格式 if (/^\d{4}-\d{2}-\d{2}$/.test(date)) { const parts = date.split('-').map(Number) d = new Date(parts[0], parts[1] - 1, parts[2]) } else { // 处理 ISO 字符串或其他格式 d = new Date(date) } } else { return '' } // 检查日期是否有效 if (isNaN(d.getTime())) return '' const now = new Date() // Reset time portion for accurate day diff const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()) const target = new Date(d.getFullYear(), d.getMonth(), d.getDate()) const diff = Math.floor((today.getTime() - target.getTime()) / 86400000) if (diff === 0) return '今天' if (diff === 1) return '昨天' if (diff === 2) return '前天' // 跨年显示年份 const isCurrentYear = d.getFullYear() === now.getFullYear() return isCurrentYear ? `${d.getMonth() + 1}月${d.getDate()}日` : `${d.getFullYear()}年${d.getMonth() + 1}月${d.getDate()}日` } export function formatMonth(date: string): string { const [year, month] = date.split('-') return `${year}年${parseInt(month)}月` } /** 获取当前月份(UTC+8 时区,与服务端保持一致) */ export function getCurrentMonth(): string { const d = new Date() const offset = 8 * 60 * 60 * 1000 const local = new Date(d.getTime() + offset) return local.toISOString().slice(0, 7) } export function formatAmountRaw(fen: number): string { const yuan = (fen / 100).toFixed(2) return yuan.replace(/\B(?=(\d{3})+(?!\d))/g, ',') }