How to overcome timezone differences in two responses and compare them

I have two responses-response A and response B.
response A has the below structure
[{
name:A
age:B
channel:[{
date:“2018-07-23T00:00:00+02:00”,
to:“2018-07-23T00:00:00+02:00”
},
{
date:“2018-07-23T00:00:00+02:00”,
to:“2018-07-23T00:00:00+02:00”
}
}]
response B has the below structure
[{
name:A
age:B
channel:[{
date:“2018-07-23T00:00:00.0+02:00”,
to:“2018-07-23T00:00:00.002:00”
},
{
date:“2018-07-23T00:00:00.0+02:00”,
to:“2018-07-23T00:00:00.0+02:00”
}
}]

When I am trying to compare both the responses under test section in response B step, my test is failing as response B has “.0” extra. Please suggest how to overcome this?

Currently I am using deepcompare
pm.expect(responseA).to.have.deep.members(resposneB)

Hey @Harsha :wave:

Maybe you can use moment to convert the timestamp to epoch timestamp and then compare (see below)?

const moment = require('moment')
let respA = [{
    date: '2018-07-23T00:00:00+02:00'
}]

let respB = [{
     date: '2018-07-23T00:00:00.0+02:00'
}]

respA.forEach((elem) => {
     elem.date = moment(elem.date).format('x')
})

respB.forEach((elem) => {
    elem.date = moment(elem.date).format('x')
})

pm.expect(respA).to.have.deep.members(respB)

Hope this helps! But let me know if you have any further questions :slightly_smiling_face:

2 Likes

Thanks for the snippet @taehoshino
Can you please suggest if the script will be working if I use the above snippet because in my original response I will have nearly 30 to 40 arrays of channel which has date inside it.

Also should I need to build a for loop inside channel ?

Hey @Harsha! Thanks for your response!

You would need to make a small modification depending on the exact response, e.g.

const channels = pm.response.json().channel
channels.forEach((elem) => {
 elem.date = moment(elem.date).format('x')
})

Hope this helps!