import { Component, OnInit, Input, OnChanges, AfterContentInit, SimpleChanges, OnDestroy } from '@angular/core'; import { TableColumn } from '../../../../models/fss-table.model'; @Component({ selector: 'fss-time-since-column', templateUrl: './time-since-column.component.html', styleUrls: ['./time-since-column.component.scss', '../../fss-column-wrapper/fss-column-wrapper.component.scss'] }) export class TimeSinceColumnComponent implements OnInit, OnChanges, AfterContentInit, OnDestroy { @Input() row: any; @Input() column!: TableColumn; @Input() value?: Date; showTime: boolean = false; days?: number; hours?: number; minutes?: number; seconds?: number; showDays: boolean = false; showHours: boolean = false; showMinutes: boolean = false; showSeconds: boolean = false; timeChangeInterval: any; constructor() { } ngOnDestroy(): void { clearInterval(this.timeChangeInterval); } ngAfterContentInit(): void { this.timeChangeInterval = setInterval(() => { this.setDisplayValue(); }, 10000); } ngOnChanges(changes: SimpleChanges): void { for (const propName in changes) { if (changes.hasOwnProperty(propName)) { switch (propName) { case 'value': this.setDisplayValue(); break; } } } } ngOnInit(): void { } setDisplayValue() { if (this.value == null) { this.showTime = false; return; } const currentDate = new Date(); let timeDifferenceInSeconds: number | undefined; if (typeof this.value.getTime === 'function') { timeDifferenceInSeconds = (currentDate.getTime() - this.value.getTime())/1000; } else if (typeof this.value === 'string' || this.value instanceof String) { const dateValue = new Date(this.value) if (dateValue.toString() !== 'Invalid Date') { timeDifferenceInSeconds = (currentDate.getTime() - dateValue.getTime())/1000; } } if (timeDifferenceInSeconds === undefined) { this.showTime = false; return; } this.days = Math.floor(timeDifferenceInSeconds / (3600 * 24)); this.hours = Math.floor(timeDifferenceInSeconds % (3600 * 24) / 3600); this.minutes = Math.floor(timeDifferenceInSeconds % 3600 / 60); this.seconds = Math.floor(timeDifferenceInSeconds % 60); this.showTime = true; } }