PDA

View Full Version : How to decode Microsoft json (date) format?



Clive Richmond
16-Nov-2022, 05:59 AM
Hi,

In an API we are attempting to consume, the date is being returned as follows;


"queue": [
{
"blob_url": "",
"create_time": "/Date(1668149153303+0000)/",
"file_name": "17603507.PDF",
"invoice_id": 17824747,
"log": "Successfully exported Supplier Invoice"

According to the developer it is Microsoft Json format and can be decoded as follows:


function parseMSDate(s) {
// Jump forward past the /Date(, parseInt handles the rest
return new Date(parseInt(s.substr(6)));
}


Does anyone have a DataFlex function, or exposed the MS function, that decodes the above value into something human readable?

Vincent Oorsprong
16-Nov-2022, 07:12 AM
Clive,

Could it be a UNIX timestamp? In that case use the DateTime library.

Clive Richmond
21-Nov-2022, 01:56 AM
Vincent,


Could it be a UNIX timestamp? In that case use the DateTime library.

Unfortunately it is not. I have found out it is Javascript. Some info from the w3schools.


JavaScript Stores Dates as Milliseconds JavaScript stores dates as number of milliseconds since January 01, 1970.

Zero time is January 01, 1970 00:00:00 UTC.

One day (24 hours) is 86 400 000 milliseconds.

Now the time is: 1669012595521 milliseconds past January 01, 1970

Using their training link (https://www.w3schools.com/js/tryit.asp?filename=tryjs_date_new_millisec)it converts the dates correctly into something humanly readable. E.g.


1668149153303 milliseconds from January 01 1970 UTC is:
Fri Nov 11 2022 14:45:53 GMT+0800 (Australian Western Standard Time)



Is there a method in the library that can do this conversion? If not, how difficult is it to add one?

Vincent Oorsprong
21-Nov-2022, 05:34 AM
Clive,

UNIX timestamps are also based on 1/1/1970 but in seconds. So in theory you could just multiply or divide by 1000 BUT in reality there is more to do as the standard functions to format date & time via the Windows API exclude the milliseconds part. The DataFlex time variable is also without milliseconds.

But I made a change in the Date & Time library and it can now use a JavaScript datetime stamp. It will be available after the build server produces a new version. It will only be available in 20.1.

Clive Richmond
23-Nov-2022, 12:26 AM
Vincent,


But I made a change in the Date & Time library and it can now use a JavaScript datetime stamp. It will be available after the build server produces a new version. It will only be available in 20.1.

Thanks for making the change. Are you able to advise when the build server will produce a new DataFlex 20.1 version that will be publicly available for download?

Vincent Oorsprong
23-Nov-2022, 03:22 AM
Clive,

The new enhanced version is available since yesterday. Have fun with it.

Clive Richmond
24-Nov-2022, 02:32 PM
Vincent,


The new enhanced version is available since yesterday. Have fun with it.

The new enhancement does exactly what we want. Thank-you. I like the new build server 👏��