# Converting Unix timestamps within Each

**URL:** <https://community.postman.com/t/converting-unix-timestamps-within-each/39575>\
**Category:** Help Hub\
**Created:** [September 28, 2022, 10:17pm UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575 "2022-09-28T22:17:27Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![getposty](https://avatars.discourse-cdn.com/v4/letter/g/8e8cbc/32.png) [@getposty](https://community.postman.com/u/getposty)\
**Post date:** [September 28, 2022, 10:17pm UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/1 "2022-09-28T22:17:27Z")

</div>

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.

```auto

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:

```auto
{
"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"
            }
      ]
    }
}

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 29, 2022, 11:40am UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/3 "2022-09-29T11:40:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![getposty](https://avatars.discourse-cdn.com/v4/letter/g/8e8cbc/32.png) [@getposty](https://community.postman.com/u/getposty)\
**Post date:** [September 29, 2022, 1:56pm UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/4 "2022-09-29T13:56:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 29, 2022, 4:39pm UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/5 "2022-09-29T16:39:54Z")

</div>

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 🙂

```auto
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
});

```

---

<div class="post-metadata">

**Author:** ![mission-cosmonaut-31](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@mission-cosmonaut-31](https://community.postman.com/u/mission-cosmonaut-31)\
**Post date:** [June 21, 2023, 2:38pm UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/6 "2023-06-21T14:38:35Z")

</div>

I try to use your advice in my template.

I use this code:

```auto
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?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 21, 2023, 3:33pm UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/7 "2023-06-21T15:33:14Z")

</div>

@mission-cosmonaut-31

> [@mission-cosmonaut-31](#):
>
> I see date in timestamp form.

That is what the original poster wanted!

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

---

<div class="post-metadata">

**Author:** ![mission-cosmonaut-31](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@mission-cosmonaut-31](https://community.postman.com/u/mission-cosmonaut-31)\
**Post date:** [June 22, 2023, 6:18am UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/8 "2023-06-22T06:18:20Z")

</div>

I have formate “startDt” = “1690675200” in response  
But I want to see human’s format as 30.07.2023 00:00 or the same

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 22, 2023, 10:17am UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/9 "2023-06-22T10:17:01Z")

</div>

@mission-cosmonaut-31

> [@mission-cosmonaut-31](#):
>
> I have formate “startDt” = “1690675200” in response  
> But I want to see human’s format as 30.07.2023 00:00 or the same

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.

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![mission-cosmonaut-31](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@mission-cosmonaut-31](https://community.postman.com/u/mission-cosmonaut-31)\
**Post date:** [June 22, 2023, 11:27am UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/10 "2023-06-22T11:27:53Z")

</div>

I can’t.  
I try to use the second method

I use this code for vizualization:

```auto
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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 22, 2023, 11:55am UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/11 "2023-06-22T11:55:07Z")

</div>

@mission-cosmonaut-31

You cannot change the elements directly using

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

```

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![mission-cosmonaut-31](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@mission-cosmonaut-31](https://community.postman.com/u/mission-cosmonaut-31)\
**Post date:** [June 22, 2023, 12:33pm UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/12 "2023-06-22T12:33:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 22, 2023, 12:48pm UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/13 "2023-06-22T12:48:21Z")

</div>

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](https://learning.postman.com/docs/introduction/resources/)

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](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-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](https://www.w3schools.com/) is a good place to start.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 22, 2023, 1:11pm UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/14 "2023-06-22T13:11:01Z")

</div>

@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).

---

<div class="post-metadata">

**Author:** ![mission-cosmonaut-31](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@mission-cosmonaut-31](https://community.postman.com/u/mission-cosmonaut-31)\
**Post date:** [June 22, 2023, 1:33pm UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/15 "2023-06-22T13:33:24Z")

</div>

It is part of response, which I need  
I want to get normal format “startDt” and “endDt”

```auto
{
    "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
}

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 22, 2023, 2:06pm UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/16 "2023-06-22T14:06:53Z")

</div>

@mission-cosmonaut-31

```auto
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
});

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/0/90a70fa402355cf1ea77efc9354450372574511c.png)

---

<div class="post-metadata">

**Author:** ![mission-cosmonaut-31](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@mission-cosmonaut-31](https://community.postman.com/u/mission-cosmonaut-31)\
**Post date:** [June 22, 2023, 2:30pm UTC](https://community.postman.com/t/converting-unix-timestamps-within-each/39575/17 "2023-06-22T14:30:39Z")

</div>

Thank you very much!  
It is work.

It’s not as hard as I thought it was before
