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 { 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 ( {formatPastDate(this.props.updated)} ); } else { const published = this.props.published; return ( {formatPastDate(published)} ); } } }