Files
xiaocai/client/src/utils/format.ts
wangxiaogang 9f5802d634 fix: 修复3个高优先级Bug + 5个中低优先级问题
BUG-01: recurring /sync next_date 更新逻辑错误(应用循环后的 next)
BUG-02: bills onReachBottom loading 缺 .value
BUG-03: group store switchToPersonal 缺少 await (3处)
OPT-04: notification requireNotificationEditAuth 加 try-catch
OPT-05: category 迁移源分类条件改为 is_custom=1 AND user_id=?
OPT-06: user 旧头像删除加 try-catch + 日志
CODE-02: format.ts 跨年日期显示年份
CODE-05: notification isValidUrl 去掉相对路径允许
2026-06-10 15:59:12 +08:00

64 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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, ',')
}