0

I am sure back end Java Rest API is sending correct date time format:

2018-05-17 19:08:25.203

Upon my Angular service receiving it, the date format becomes a large number:

1526555305203

My Angular service is calling REST API as below:

this.configurationService.retrieveTableData(this.schemaFullname, this.tableName).subscribe(data => {

//READ data here

})

How should I preserve the original format that REST API sent me?

xzk
  • 827
  • 2
  • 18
  • 43

2 Answers2

1

It returns a timestamp as a string, you can convert back to date on your angular application as,

import { DatePipe } from '@angular/common';

const datePipe = new DatePipe('en-US');
const myFormattedDate = datePipe.transform(this.myTimeStamp, 'EEEE, MMMM d');
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Accepted your answer as it provided a solution, rather than a concept that I already knew of. I used another reference as well during my implementation: https://stackoverflow.com/questions/47783376/how-to-use-pipes-in-component – xzk May 19 '18 at 03:58
1

This "number," as you wrote, is only a way of determining the current time, usually in seconds, or in milliseconds (in your case) or sometimes even in microseconds, calculated from 1/1/1970, 00:00 (UNIX Epoch Time) . You can create a conversion function yourself or use any built-in feature. Functions to convert this so-called "Unix Time" from / to real time exist almost in all new programming languages. For example, you can simply calculate the current year as follows:

1970 + (1526555305203 / 1000 / 60 / 60 / 24 / 365.25) = 2 018.373619

For clarification ... every fourth year comes the transfer month - February - it has 1 day more. That's why we can improve this value. We will increase the number of days in each year by 0.25 per day, so: 4 years * 0.25 day = 1 day, so instead of 365 days we would prefer 365.25 (that is, after four years we get an extra full day: 365.25 * 4 = 1461 ; 365 * 4 = 1460 .

For more info: https://en.wikipedia.org/wiki/Unix_time

s3n0
  • 596
  • 3
  • 14