I have implemented a custom getDate() method in my Angular component
getDate(date: Date){
return date.format("dd-MMMM-yyyy HH:mm").toString();
}
I am retrieving string JSON data from the database in the following format:
`
"{"field":"date","oldValue":"Mon Mar 25 00:00:00 GMT 2019","newValue":"Tue Mar 26 00:00:00 GMT 2019"}, {"field":"techniqueType","oldValue":"Intra","newValue":"Intralesional"}
`
The getList method in my code looks like this:
getList(patientId?: number, pageNo?: number) {
const auditTrailSubscription =
this._auditTrailService.getAuditTrailUrl(patientId, pageNo,
GridConfig.ITEMS_PER_PAGE)
.subscribe(
result => {
try {
this.results = [];
this.totalItems = result.recordsCount;
this.auditTrailList = result.lstRecords;
this.auditTrailList.forEach(x => {
let jschanges = JSON.parse(`[${x.jsonChanges}]`);
jschanges.forEach(z => {
this.results.push({
userName: x.userName,
timestamp: x.timestamp,
entityName: x.entityName,
field: z.field,
oldValue: z.oldValue instanceof Date ?
this.getDate(z.oldValue) : z.oldValue,
newValue: z.newValue instanceof Date ?
this.getDate(z.newValue) : z.newValue
});
});
});
} catch (e) {
console.log(e);
}
},
err => {
this.handleError(err);
}
);
this.addSubscription("auditTrail", auditTrailSubscription);
}
Html file
<tr *ngFor="let at of results | paginate: { itemsPerPage:
_ITEMS_PER_PAGE, currentPage: crtPage, totalItems: totalItems }"
[attr.data-row-id]="at.userId">
<td>{{ at.userName }}</td>
<td>{{ at.timestamp ? (at.timestamp | date:
AUDIT_TRAIL_DATE_TIME_FORMAT ) : 'Unknown' }}</td>
<td>{{ at.entityName }}</td>
<td>{{ at.field | titlecase }}</td>
<td>{{ at.oldValue }}</td>
<td>{{ at.newValue }}</td>
</tr>
In order to handle different types for `oldValue` and `newValue`, I utilize my custom getDate() method to ensure proper formatting.