lemmy-ui/src/shared/components/common/moment-time.tsx
Dessalines a2716d5f48
Fixing timezone issue with date-fns. Fixes #1680 (#1681)
Co-authored-by: Alec Armbruster <35377827+alectrocute@users.noreply.github.com>
2023-06-28 23:09:25 -04:00

62 lines
1.7 KiB
TypeScript

import { capitalizeFirstLetter, formatPastDate } from "@utils/helpers";
import { formatInTimeZone } from "date-fns-tz";
import parseISO from "date-fns/parseISO";
import { Component } from "inferno";
import { I18NextService } from "../../services";
import { Icon } from "./icon";
interface MomentTimeProps {
published: string;
updated?: string;
showAgo?: boolean;
ignoreUpdated?: boolean;
}
function formatDate(input: string) {
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
const parsed = parseISO(input + "Z");
return formatInTimeZone(parsed, tz, "PPPPpppp");
}
export class MomentTime extends Component<MomentTimeProps, any> {
constructor(props: any, context: any) {
super(props, context);
}
createdAndModifiedTimes() {
const updated = this.props.updated;
let line = `${capitalizeFirstLetter(
I18NextService.i18n.t("created")
)}: ${formatDate(this.props.published)}`;
if (updated) {
line += `\n\n\n${capitalizeFirstLetter(
I18NextService.i18n.t("modified")
)} ${formatDate(updated)}`;
}
return line;
}
render() {
if (!this.props.ignoreUpdated && this.props.updated) {
return (
<span
data-tippy-content={this.createdAndModifiedTimes()}
className="moment-time fst-italic pointer unselectable"
>
<Icon icon="edit-2" classes="icon-inline me-1" />
{formatPastDate(this.props.updated)}
</span>
);
} else {
const published = this.props.published;
return (
<span
className="moment-time pointer unselectable"
data-tippy-content={formatDate(published)}
>
{formatPastDate(published)}
</span>
);
}
}
}