import { LightningElement, api } from "lwc"; export default class RelativeDateTime extends LightningElement { @api set value(value) { if (!value) { console.error("Value provided to RelativeDateTime is invalid"); return; } this._inputDateTime = new Date(value); } get value() { this._relativeDateTime = this.getFormattedDateTime( this._inputDateTime! ); return this._relativeDateTime; } get original() { return this._inputDateTime?.toLocaleString(); } private _inputDateTime?: Date; private _relativeDateTime?: string; getFormattedDateTime(inputDateTime: Date) { const now = new Date(); const yesterday = new Date( now.getTime() - 24 * 60 * 60 * 1000 ).toLocaleDateString(); const twoDaysAgo = new Date( now.getTime() - 48 * 60 * 60 * 1000 ).toLocaleDateString(); const dateStringFromInput = inputDateTime?.toLocaleDateString(); if (dateStringFromInput === yesterday) { return "1 day ago"; } else if (dateStringFromInput === twoDaysAgo) { return "2 days ago"; } return dateStringFromInput; } }