/** * 前端格式化工具:数字、百分比、日期、时长。 * 业务代码统一走这里,避免散落重复的格式化逻辑。 */ export const formatPercent = (value: number) => `${Math.round(value)}%` /** 秒 → 「X 分 Y 秒」或「Y 秒」 */ export function formatDuration(seconds: number): string { const s = Math.max(0, Math.round(seconds)) if (s < 60) return `${s} 秒` const m = Math.floor(s / 60) const rest = s % 60 return rest === 0 ? `${m} 分钟` : `${m} 分 ${rest} 秒` } /** YYYY-MM-DD → 「M月D日」 */ export function formatDateShort(iso: string): string { const [, m, d] = iso.split('-') return `${Number(m)}月${Number(d)}日` } /** 分钟 → 「1小时25分」/「45分」/「0分」 */ export function formatMinutes(mins: number): string { const m = Math.max(0, Math.round(mins)) if (m < 60) return `${m}分` const h = Math.floor(m / 60) const rest = m % 60 return rest === 0 ? `${h}小时` : `${h}小时${rest}分` } /** 按当天时段返回问候语(早上好 / 下午好 / 晚上好) */ export function greetingByHour(hour = new Date().getHours()): string { if (hour < 6) return '夜深了' if (hour < 12) return '早上好' if (hour < 18) return '下午好' return '晚上好' }