1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-12-23 15:14:23 +00:00
festa/components/extensions/FestaMoment.tsx

39 lines
928 B
TypeScript
Raw Normal View History

2022-06-08 15:31:34 +00:00
import { useTranslation } from "next-i18next"
2022-06-09 22:22:47 +00:00
type FestaMomentProps = {
2022-06-10 03:21:02 +00:00
date: Date | null,
2022-06-08 15:31:34 +00:00
}
2022-06-09 21:55:49 +00:00
/**
* Component that formats a {@link Date} to a machine-readable and human-readable HTML `time[datetime]` element.
*/
2022-06-09 22:22:47 +00:00
export function FestaMoment({ date }: FestaMomentProps) {
2022-06-09 21:55:49 +00:00
const { t } = useTranslation()
2022-06-08 15:31:34 +00:00
2022-06-10 03:21:02 +00:00
if (!date || Number.isNaN(date.getTime())) {
2022-06-08 15:31:34 +00:00
return (
<span className="disabled">
{t("dateNaN")}
</span>
)
}
2022-06-10 03:21:02 +00:00
const now = new Date()
const machine = date.toISOString()
let human
// If the date is less than 24 hours away, display just the time
if (date.getTime() - now.getTime() < 86_400_000) {
human = date.toLocaleTimeString()
}
// Otherwise, display the full date
else {
human = date.toLocaleString()
}
2022-06-08 15:31:34 +00:00
return (
2022-06-10 03:21:02 +00:00
<time dateTime={machine}>
{human}
2022-06-08 15:31:34 +00:00
</time>
)
}