After retrieving a list from my API, the data looks like this:
{
"content": [
{
"trainingClassId": 1,
"examCode": "my exam 1",
"address": {
"addressId": 1,
"addressValue": "abc 1213"
},
"description": "only test",
"classId": null,
"startDate": 1511110800000,
"endDate": 1513702800000,
"examDate": 1511542800000
},
{
"trainingClassId": 2,
"examCode": "my exam 2",
"address": {
"addressId": 1,
"addressValue": "abc 1213"
},
"description": "only test",
"classId": null,
"startDate": 1511110800000,
"endDate": 1513702800000,
"examDate": 1511542800000
}
],
"last": true,
"totalElements": 2,
"totalPages": 1,
"size": 20,
"number": 0,
"first": true,
"sort": null,
"numberOfElements": 2
}
To convert long timestamps to date format, I've created a data binding object like this:
export class myApp {
id: number;
classId: string;
trainingDate: string;
examDate: string;
}
In my TypeScript file:
listData = new Array();
app_unit:myApp= new myApp();
listApp:any[];
ngOnInit() {
this.getAllTrainingClass();
}
async getAllTrainingClass(): Promise<void>{
await this.traningClassService.getdata().then(data =>this.listApp = data);
for(let ls of this.listApp){
this.app_unit.classId = ls.classId;
this.app_unit.examDate=this.convertTimestampToDate(ls.examDate);
this.app_unit.trainingDate=this.convertTimestampToDate(ls.startDate) +'-'+this.convertTimestampToDate(ls.endDate) ;
this.listData.push(this.app_unit);
}
The console log displays 2 items, but they are both the latest item, shown here:
{0:myApp id:2 classId:null examDate:"05/01/2018" trainingDate:"05/11/2017-31/12/2017" }, {1:myApp id:2 classId:null examDate:"05/01/2018" trainingDate:"05/11/2017-31/12/2017"}
Please provide me with some advice on this situation.