Is there a way to convert "Timestamp" say "2020-08-06T08:18:12.500Z" to "UTCTimestamp": "1596701892" or Vice versa?

My postman response body looks something like this:

{
    "GetServerTimeResponse": {
        "Timestamp": "2020-08-06T08:18:12.500Z",
        "UTCTimestamp": "1596701892",
    }
}

How to make sure Timestamp and UTCTimestamp are equal?

Either I can convert Timestamp to UTCTimestamp and say both strings match or convert UTCTimestamp to Timestamp and say both strings are matching?

In Java langauge, I know this is how we do. But would be interested to know how to do in postman scripts:

//Verify time
String timeStamp = new Long(responseType.getTimestamp().getTimeInMillis()/1000).toString();
String UTCTimestamp = responseType.getUTCTimestamp();
test.assertValue("TimeStamps are equal", timeStamp, UTCTimestamp);

Hey @avinash.balekundri

Welcome to the community and thank you for posting the question! :robot:

Postman comes with moment built-in which is awesome for working with dates and times in Javascript.

You could use something like this to convert the 2 timestamps and then use the isSame() method to do the compression.

let moment = require('moment'),
    jsonData = pm.response.json()
    Timestamp = jsonData.GetServerTimeResponse.Timestamp,
    UTCTimestamp = jsonData.GetServerTimeResponse.UTCTimestamp

console.log(moment.unix(UTCTimestamp).isSame(moment(Timestamp)))

I’ve just logged that out to the console for now but you could also wrap that in a test:

pm.test("Timestamps are equal", () => {
    pm.expect(moment.unix(UTCTimestamp).isSame(moment(Timestamp))).to.be.true
})
1 Like

thanks danny. It worked for me. This is what i was looking for.

1 Like

Hi guys,
on a similar theme, is there a way to validate that the timestamp in a response is in the unix timestamp format?
We currently just verify that the timestamp field is returned:
});

pm.test(“Body matches string”, function () {

pm.expect(pm.response.text()).to.include("timestamp");

});

but we now need to check that the format is correct, ie “timestamp”: 1624007506

many thanks for any help on this!

Graham

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.