2023-02-18 13:11:01 +03:00
|
|
|
import { format as formatDate, formatDistance, getUnixTime, parseISO } from 'date-fns';
|
2022-12-05 19:18:00 +03:00
|
|
|
import { isDateObject, now, STANDARD_DATE_AND_TIME_FORMAT } from '../helpers/date';
|
2021-06-24 21:13:06 +03:00
|
|
|
|
2022-05-29 21:32:34 +03:00
|
|
|
export interface TimeProps {
|
2021-06-24 21:13:06 +03:00
|
|
|
date: Date | string;
|
|
|
|
format?: string;
|
|
|
|
relative?: boolean;
|
|
|
|
}
|
|
|
|
|
2022-10-23 11:43:01 +03:00
|
|
|
export const Time = ({ date, format = STANDARD_DATE_AND_TIME_FORMAT, relative = false }: TimeProps) => {
|
2021-06-24 21:13:06 +03:00
|
|
|
const dateObject = isDateObject(date) ? date : parseISO(date);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<time dateTime={`${getUnixTime(dateObject)}000`}>
|
2022-12-05 19:18:00 +03:00
|
|
|
{relative ? `${formatDistance(now(), dateObject)} ago` : formatDate(dateObject, format)}
|
2021-06-24 21:13:06 +03:00
|
|
|
</time>
|
|
|
|
);
|
|
|
|
};
|