obs-portal/frontend/src/components/FormattedDate.tsx
2022-07-28 13:56:18 +02:00

26 lines
642 B
TypeScript

import {DateTime} from 'luxon'
import {useTranslation} from 'react-i18next'
export default function FormattedDate({date, relative = false}) {
if (date == null) {
return null
}
const dateTime =
typeof date === 'string' ? DateTime.fromISO(date) : date instanceof Date ? DateTime.fromJSDate(date) : date
let str
const {i18n} = useTranslation()
const locale = i18n.language
if (relative) {
str = dateTime.setLocale(locale).toRelative()
} else {
str = dateTime.setLocale(locale).toLocaleString(DateTime.DATETIME_MED)
}
const iso = dateTime.toISO()
return <time dateTime={iso} title={iso}>{str}</time>
}