import { AfterContentInit, Component, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core'; import { TableColumn } from '../../../../models/fss-table.model'; import moment from 'moment'; @Component({ selector: 'fss-date-column', templateUrl: './fss-date-column.component.html', styleUrls: ['./fss-date-column.component.scss', '../../fss-column-wrapper/fss-column-wrapper.component.scss'] }) export class FssDateColumnComponent implements OnInit, AfterContentInit, OnChanges { @Input() row: any; @Input() column!: TableColumn; @Input() value?: Date | string; hasCustomSetting = false; defaultFormat = 'MM/dd/yy hh:mm:ss a'; momentDate: string = ''; constructor() { } ngOnChanges(changes: SimpleChanges): void { for (const propName in changes) { if (changes.hasOwnProperty(propName)) { switch (propName) { case 'column': case 'value': this.formatDate(); break; } } } } ngAfterContentInit(): void { this.formatDate(); } ngOnInit(): void { } formatDate() { let timeFormat: string = ''; if (this.column.timeFormat) { this.hasCustomSetting = true; timeFormat = this.column.timeFormat; } let dateFormat: string = ''; if (this.column.dateFormat){ this.hasCustomSetting = true; dateFormat = this.column.dateFormat; } if (this.hasCustomSetting && this.value != null) { const date = moment(this.value); if (date.isValid()) { this.momentDate = moment(this.value).format(`${dateFormat} ${timeFormat}`); } else { this.momentDate = ''; } } else { this.momentDate = ''; } } }