Converting Unix timestamps within Each

Iā€™m using an html template within the Tests tab to output my json data, however, some of the data is in timestamp form. Namely, the date_created, first_seen, & last_seen are all being returned in timestamp form. Is there a way to do the calculation to a more standard date/time format from within the var template ? Iā€™ve tried several ways to convert the timestamp, but none work within the template variable or cause the call to fail. Is it even possible to do within that template? Or how would you do it outside the template and then pull it back into the html? Because of the each loop Iā€™m just not sure how to do this.
The other thing to keep in mind is the number of number of results can vary, so itā€™s not a fixed number of results I can hard code.


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;}
        .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>
    
            Dom: {{response.result.dom}}</br>
            Rep: {{response.result.rep}}</br>
            Reg: {{response.result.reg}}</br>
            Date Created: {{response.result.date_created}}</br>
            First Seen: {{response.result.first_seen}}</br>
            Last Seen: {{response.result.last_seen}}</br>
    <table class="tftable" border="1">
        <tr>
            <th>Name</th>
            <th>Rep</th>
            <th>First Seen</th>
            <th>Last Seen</th>
        </tr>
        <tr>

        </tr>
        {{#each response.result.ns}}
            <tr id=row_{{@key}} onClick="handleClick(this.id)">
                <td id={{@key}}>{{host}}</td>
                <td>{{rep}}</td>
                <td>{{first_seen}}</td>
                <td>{{last_seen}}</td>
            </tr>
        {{/each}}
               {{#each response.result.send}}
            <tr id=row_{{@key}} onClick="handleClick(this.id)">
                <td id={{@key}}>{{id}}</td>
                <td></td>
                <td></td>
                <td>{{last_seen}}</td>
            </tr>
        {{/each}}
    </table>
`;

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

The JSON response is like:

{
"code": 200,
    "result": {
        "dom": "abc",
        "rep": "green",
        "reg": "Going",
        "date_created": 1311292800,
        "first_seen": 1339185300,
        "last_seen": 1661781780,
        "ns": [
            {
                "host": "aws",
                "first_seen": 1517597640,
                "last_seen": 1661781780,
                "rep": "green"
            }
      ]
    }
}

Can you post an example of your response?

Iā€™m not sure you can call a function to change the date with your variable, as it will probably just save it as text as part of the overall string. (But Iā€™ll have a go).

What is the current format of the date? And what does it need to look like?

Apologies on that oversight. Iā€™ve updated the post to add a sample json response with the format of the timestamps theyā€™re passing back.

You need to parse the response first to a JavaScript object which you can then update before using in the visualizer. The following will update the initial elements but not the ones in the ā€œnsā€ array. You will probably need to extend the code to loop though the array and create a new array to then use in the visualizer. I canā€™t think of any other way to do this. This is more of a JavaScript problem than Postman. So if you know a JavaScript developer, I would lean on them :slight_smile:

json = 
    {
    "code": 200,
        "result": {
            "dom": "abc",
            "rep": "green",
            "reg": "Going",
            "date_created": 1311292800,
            "first_seen": 1339185300,
            "last_seen": 1661781780,
            "ns": [
                {
                    "host": "aws",
                    "first_seen": 1517597640,
                    "last_seen": 1661781780,
                    "rep": "green"
                }
        ]
        }
    }

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;}
        .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>
    
            Dom: {{response.result.dom}}</br>
            Rep: {{response.result.rep}}</br>
            Reg: {{response.result.reg}}</br>
            Date Created: {{response.result.date_created}}</br>
            First Seen: {{response.result.first_seen}}</br>
            Last Seen: {{response.result.last_seen}}</br>
    <table class="tftable" border="1">
        <tr>
            <th>Name</th>
            <th>Rep</th>
            <th>First Seen</th>
            <th>Last Seen</th>
        </tr>
        <tr>

        </tr>
        {{#each response.result.ns}}
            <tr id=row_{{@key}} onClick="handleClick(this.id)">
                <td id={{@key}}>{{host}}</td>
                <td>{{rep}}</td>
                <td>{{first_seen}}</td>
                <td>{{last_seen}}</td>
            </tr>
        {{/each}}
               {{#each response.result.send}}
            <tr id=row_{{@key}} onClick="handleClick(this.id)">
                <td id={{@key}}>{{id}}</td>
                <td></td>
                <td></td>
                <td>{{last_seen}}</td>
            </tr>
        {{/each}}
    </table>
`;

var date_created = (new Date(json.result.date_created * 1000)).toISOString().slice(0, 10);
var first_seen = (new Date(json.result.first_seen * 1000)).toISOString().slice(0, 10);
var last_seen = (new Date(json.result.last_seen * 1000)).toISOString().slice(0, 10);

json.result.date_created = date_created;
json.result.first_seen = first_seen;
json.result.last_seen = last_seen;

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

I try to use your advice in my template.

I use this code:

var startDt = (new Date(pm.response.json().tournament.blockHeader.startDt * 1000)).toISOString().slice(0, 10);
var endDt = (new Date(pm.response.json().tournament.blockHeader.endDt * 1000)).toISOString().slice(0, 10);

pm.response.json().tournament.blockHeader.startDt = startDt;
pm.response.json().tournament.blockHeader.endDt = endDt;
     pm.visualizer.set(template, {response: pm.response.json()});

But it doesnā€™t work. I see date in timestamp form.
Whatā€™s my mistakes?

@mission-cosmonaut-31

That is what the original poster wanted!

What format do you want it in? (And what format is the date currently in).

I have formate ā€œstartDtā€ = ā€œ1690675200ā€ in response
But I want to see humanā€™s format as 30.07.2023 00:00 or the same

@mission-cosmonaut-31

So itā€™s the same principle. The date is in seconds format, so needs to be converted first before using moment to format the date. (Or you can use the slice method if you only want the date).

Either by a function or the toISOstring method.

function toDateTime(secs) {
    var t = new Date(Date.UTC(1970, 0, 1)); // Epoch
    t.setUTCSeconds(secs);
    return t;
}

let startDt = "1690675200" 
var moment = require('moment');

// function method


var formattedDate = moment(toDateTime(startDt)).format("DD.MM.YYYY hh:mm"); // 30.07.2023 00:00
console.log(formattedDate);

// toISOstring method

let toISOstring = (new Date(startDt * 1000)).toISOString();
var formattedDate2 = moment(toISOstring).format("DD.MM.YYYY hh:mm"); // 30.07.2023 00:00
console.log(formattedDate2);

I canā€™t.
I try to use the second method

I use this code for vizualization:

var moment = require('moment');
let startDt = (new Date(pm.response.json().tournament.blockHeader.startDt * 1000)).toISOString();

pm.response.json().tournament.blockHeader.startDt = moment(startDt).format("DD.MM.YYYY hh:mm");
     pm.visualizer.set(template, {response: pm.response.json()});

And I use this string:

<p><b>Start Date: </b>{{response.tournament.blockHeader.startDt}}</p>

But I get this result: Start Date: 1690675200

@mission-cosmonaut-31

You cannot change the elements directly using

pm.response.json().tournament.blockHeader.startDt =

You need to parse the response to a variable and manipulate that.

let response = pm.response.json();

Otherwise each time you call pm.response.json(), it will just get the original response. Which is why your date is still showing showing the original timestamp.

I donā€™t understand, where I need to use this parser

Now I use pm.visualizer.set(template, {response: pm.response.json()}) for vizualization
And I use responseā€™s variable {{}} for all my parameters, not only for dates

Where do I need to use parser? Near pm.visualizer.set(template, {response: pm.response.json()}) ?

Or I can use it in html parts?

Sorry for stupid questions. Iā€™m beginner in Postman

It should be your first line of code.

Then anywhere you have pm.response.json(), you just reference the variable instead.

If you are new to Postman I would recommend the Postman training links which are located under ā€œother resourcesā€ in the Learning Centre.

Other resources | Postman Learning Center

The ā€œGalaxy APIā€™s 101ā€ course gets you used to sending requests and the GUI.

The ā€œGalaxy Testing and Automationā€ gets you used to testing your responses and using variables, basic scripting, etc.

Also take a look at the following.

Postman JavaScript reference | using-environment-variables-in-scripts

Finally, Postman uses JavaScript under the hood, If you are going to be scripting, I would also recommend learning some JavaScript basics. WC3 schools is a good place to start.

1 Like

@mission-cosmonaut-31

If you post an example response, I will show you the code to get the visualiser working properly.

Please use the ā€˜preformatted textā€™ option in the editor (so the code doesnā€™t all align to the left).

It is part of response, which I need
I want to get normal format ā€œstartDtā€ and ā€œendDtā€

{
    "tournament": {
"id": 404,
        "kind": 2,
        "type": 1,
        "providerTournamentWithStages": true,
        "status": 1,
        "blockHeader": {
            "title": "Name",
            "sum": 100500,
            "currencyId": 362,
            "mediaValue": "/genfiles/cms/desktop/tournament/5b046dcf4c6449c090fd723bde87ff5f.png",
            "startDt": 1690675200,
            "endDt": 1692230400,
            "description": "description",
            "prizeTitle": "prizeTitle"

        "meParticipating": false
    },
    "errorId": 0
}

@mission-cosmonaut-31

let response = pm.response.json() // parse response

console.log(response); // original response

let startDate = response.tournament.blockHeader.startDt; // in seconds
let endDate = response.tournament.blockHeader.endDt // in seconds

// update date format before being visualised
response.tournament.blockHeader.startDt = (new Date(startDate * 1000)).toISOString().slice(0, 10);
response.tournament.blockHeader.endDt = (new Date(endDate * 1000)).toISOString().slice(0, 10);

console.log(response); // updated response

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>Title</th>
            <th>Start Date</th>
            <th>End Date</th>
        </tr>

        {{#each response}}
            <tr>
                <td>{{blockHeader.title}}</td>
                <td>{{blockHeader.startDt}}</td>
                <td>{{blockHeader.endDt}}</td>
            </tr>
        {{/each}}

    </table>

`;

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

1 Like

Thank you very much!
It is work.

Itā€™s not as hard as I thought it was before