VetNet-LCPS / vetcopilot-app /src /utils /dateUtils.ts
thomasm291678
VetCopilot-LCPS: LCPS 2.0 本地推理引擎替换 DeepSeek API
793e953
Raw History Blame Contribute Delete
1.39 kB
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)
}