refactor: improve date handling and timezone consistency

- Add timezone support to database module (+08:00)
- Extract date formatting utilities to shared modules
- Standardize timezone handling across frontend and backend
- Improve date formatting consistency in UI components
- Refactor crawler page.goto options for better readability
This commit is contained in:
dmy
2026-01-15 16:17:41 +08:00
parent eba5c7e5c5
commit 5edebd9d55
20 changed files with 219 additions and 57 deletions

View File

@@ -0,0 +1,58 @@
/**
* 日期格式化工具函数
* 统一处理东八区(Asia/Shanghai)时间显示
*/
/**
* 格式化日期为 YYYY-MM-DD 格式
* @param dateStr 日期字符串或Date对象
* @returns 格式化后的日期字符串
*/
export function formatDate(dateStr: string | null | undefined): string {
if (!dateStr) return '-'
const date = new Date(dateStr)
if (isNaN(date.getTime())) return '-'
return date.toLocaleDateString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
timeZone: 'Asia/Shanghai'
}).replace(/\//g, '-')
}
/**
* 格式化日期时间为 YYYY-MM-DD HH:mm 格式
* @param dateStr 日期字符串或Date对象
* @returns 格式化后的日期时间字符串
*/
export function formatDateTime(dateStr: string | null | undefined): string {
if (!dateStr) return '-'
const date = new Date(dateStr)
if (isNaN(date.getTime())) return '-'
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
timeZone: 'Asia/Shanghai'
}).replace(/\//g, '-')
}
/**
* 格式化日期为简洁的 YYYY-MM-DD 格式(用于置顶项目等)
* @param dateStr 日期字符串或Date对象
* @returns 格式化后的日期字符串
*/
export function formatSimpleDate(dateStr: string | null | undefined): string {
if (!dateStr) return ''
const date = new Date(dateStr)
if (isNaN(date.getTime())) return ''
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}