Postman change unix time in microseconds to datetime

175 Views Asked by At

I have a JSON response which includes unix timestamp in microseconds. I want to either: a. Add a field to the response so that it shows the date+time; or b. Change the field itself to be date+time.

Whichever is easier, but preferably a. The format I need is DD/MM/YYYY HH:MM:SS.ssssss

I was reviewing the following threads but I don't really understand them:

Convert UTC Epoch to local date

https://community.postman.com/t/convert-datetime-format-of-response-from-epoch-to-datetime/35320

https://learning.postman.com/docs/postman-flows/reference/working-with-date-and-time/

And I tried looking at FQL documentation but that was too in-depth too.

This is part of an example response:

"SequenceNumber": 230901153510049870,
"Trade": {
        "LastPrice": 73.6,
        "LastSize": 53798,
        "Timestamp": 1693582510115510,

And I would like it to look something like:

"SequenceNumber": 230901153510049870,
"Trade": {
        "LastPrice": 73.6,
        "LastSize": 53798,
        "Timestamp": 1693582510115510,
      **  "DateTime": 01/09/2023 15:35:10.115510,**

Full example output:

{
    "ErrorCode": 0,
    "Outcome": "Success",
    "Ticks": [
        {
            "SequenceNumber": 230814040000019593,
            "IsExtendedHour": false,
            "Ask": null,
            "Bid": null,
            "Trade": {
                "LastPrice": 74700.0,
                "LastSize": 100,
                "Timestamp": 1691985600000000,
                "OptionalFields": {
                    "TradeId": "58850000",
                    "size": "100",
                    "price": "74700",
                    "style": "-1",
                    "type": "0",
                    "minorcurrencytype": "-1",
                    "currency": "964",
                    "marketsession": "0"
                }
            }
        },
        {
            "SequenceNumber": 230814040000019591,
            "IsExtendedHour": false,
            "Ask": null,
            "Bid": null,
            "Trade": {
                "LastPrice": 74700.0,
                "LastSize": 200,
                "Timestamp": 1691985600000000,
                "OptionalFields": {
                    "TradeId": "58840000",
                    "size": "200",
                    "price": "74700",
                    "style": "-1",
                    "type": "0",
                    "minorcurrencytype": "-1",
                    "currency": "964",
                    "marketsession": "0"
                }
            }
        }
    ],
    "DepthOfBooks": []
}
1

There are 1 best solutions below

0
mikee On

In the following example, I'm looping through the parsed response updating the Timestamp before its consumed in the Visualizer.

I had to slice the timestamp to get it to show as you requested. (I'm still not sure what the last six digits are).

const response = pm.response.json();

var moment = require('moment');

response.Ticks.forEach(obj => {
    let utcMicroseconds = obj.Trade.Timestamp.toString();
    let utcSeconds = parseInt(utcMicroseconds.slice(0, 10)) * 1000;
    let remainder = parseInt(utcMicroseconds.slice(-6));
    let d = moment(utcSeconds).format("DD/MM/YYYY hh:mm:ss");
    obj.Trade.Timestamp = d + "." + remainder;
});

var template = `

    <style type="text/css">
        .tftable {font-size:14px;color:#333333;width:100%;border-width: 1px;border-color: #87ceeb;border-collapse: collapse;}
        .tftable th {font-size:18px;background-color:#87ceeb;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;text-align:left;position:sticky;top:0;}
        .tftable tr {background-color:#ffffff;}
        .tftable td {font-size:14px;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;}
        .tftable tr:hover {background-color:#e0ffff;}
    </style>
    
    <table class="tftable" border="1">

        <tr>
            <th>Sequence Number</th>
            <th>Timestamp</th>
        </tr>

        {{#each response}}
            <tr>
                <td>{{SequenceNumber}}</td>
                <td>{{Trade.Timestamp}}</td>
            </tr>
        {{/each}}

    </table>

`;

pm.visualizer.set(template, {
    response: response.Ticks
});

This produces the following in the Visualizer. (Please note, the time is showing my timezone, so its an hour out).

enter image description here