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