import { useTranslation } from '../i18n' export function useDateFormatter() { const { locale } = useTranslation() const dateLocale = (): string => (locale === 'en' ? 'en-US' : 'zh-CN') const formatDate = (date: Date | string, options?: Intl.DateTimeFormatOptions) => { const d = typeof date === 'string' ? new Date(date) : date return d.toLocaleDateString(dateLocale(), options) } const formatDateTime = (date: Date | string, options?: Intl.DateTimeFormatOptions) => { const d = typeof date === 'string' ? new Date(date) : date return d.toLocaleString(dateLocale(), options) } const formatTime = (date: Date | string) => { const d = typeof date === 'string' ? new Date(date) : date return d.toLocaleTimeString(dateLocale()) } return { formatDate, formatDateTime, formatTime } } /** 纯函数版:不需要 hook,传入 locale 即可 */ export function formatDateStatic(date: Date | string, locale: string, options?: Intl.DateTimeFormatOptions) { const d = typeof date === 'string' ? new Date(date) : date return d.toLocaleDateString(locale === 'en' ? 'en-US' : 'zh-CN', options) } export function formatDateTimeStatic(date: Date | string, locale: string, options?: Intl.DateTimeFormatOptions) { const d = typeof date === 'string' ? new Date(date) : date return d.toLocaleString(locale === 'en' ? 'en-US' : 'zh-CN', options) }