Use local date format.

This commit is contained in:
Michael Zhang 2023-03-31 18:22:34 -05:00
parent bd2b79c84d
commit 7e1b747a37
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B

View file

@ -1,14 +1,18 @@
import React from "react";
import PropTypes from "prop-types";
import dateFormat from "dateformat";
import { isInSameDay } from "../../../util/common";
function Time({ timestamp, fullTime }) {
export interface TimeProps {
timestamp: number;
fullTime: boolean;
}
function Time({ timestamp, fullTime }: TimeProps) {
const date = new Date(timestamp);
const formattedFullTime = dateFormat(date, "dd mmmm yyyy, hh:MM TT");
let formattedDate = formattedFullTime;
let formattedDateTime = formattedFullTime;
if (!fullTime) {
const compareDate = new Date();
@ -16,18 +20,21 @@ function Time({ timestamp, fullTime }) {
compareDate.setDate(compareDate.getDate() - 1);
const isYesterday = isInSameDay(date, compareDate);
formattedDate = dateFormat(
const dtf = new Intl.DateTimeFormat();
const formattedDate = dtf.format(date);
formattedDateTime = dateFormat(
date,
isToday || isYesterday ? "hh:MM TT" : "dd/mm/yyyy"
isToday || isYesterday ? "hh:MM TT" : formattedDate
);
if (isYesterday) {
formattedDate = `Yesterday, ${formattedDate}`;
formattedDateTime = `Yesterday, ${formattedDateTime}`;
}
}
return (
<time dateTime={date.toISOString()} title={formattedFullTime}>
{formattedDate}
{formattedDateTime}
</time>
);
}
@ -36,9 +43,4 @@ Time.defaultProps = {
fullTime: false,
};
Time.propTypes = {
timestamp: PropTypes.number.isRequired,
fullTime: PropTypes.bool,
};
export default Time;