Compare data of the same response

Hi guys!

I need help with something that is probably very simple, i just dont know how to do it.

I want to do a test where i compare the date of creation with the updated. For example, if i get a response with:

“createdAt”: “1965-07-12”,
“updatedAt”: “1956-06-25”

i want the test to fail, due to the date of creation being higher than of the date of being updated. Any tips?

Thanks a bunch!

Hi @shaga85

Have a look at this, it should give you what you need;

Wow, sounds amazing, the only thing is that i still dont have the knowledge to make it work on my side.

My body response is:

{
"application": {
"id": "ad",
"name": "ullamco incididunt est in esse",
"owner": {
"id": "eiusmod sed occaecat irure",
"name": "deserunt",
"email": "exercitation nostrud incididunt dolore",
"msisdn": "velit",
"createdAt": "2003-12-08",
"updatedAt": "1966-07-16"
},
"createdAt": "1965-07-12",
"updatedAt": "1956-06-25"
},
"id": "non labore commodo esse et",
"address": "ut",
"name": "magna ad irure labore",
"symbol": "cupidatat esse Ut ut",
"type": "ERC721",
"ownerAddress": "minim nisi",
"blockchain": {
"id": "velit sunt",
"name": "nostrud sed consequat dolor",
"explorerUri": "dolore quis id",
"code": -50015631.7740315
},
"baseUri": "qui sed proident",
"active": true,
"status": "pending",
"createdAt": "1957-07-04",
"updatedAt": "1945-06-08"
}

So, if i wanted to compare the dates (application.createdAt(1965-07-12)) and (application.updatdAt(1956-06-25)) with the script you sent, what do i have to do?
Thanks for all the help!! I really appreciate it!

Using @mdjones approach linked above, it would be something like this;

//Import moment library
let moment = require ('moment');
//Grab the entire response
const response = pm.response.json();

//parseInt() function parses a string argument and returns an integer
//we need a number so we can calculate less/greater than
let createdAt = parseInt(moment(response.application.createdAt).format("YYYYMMDD"));
//log to console 
console.log(createdAt);

let updatedAt = parseInt(moment(response.application.updatedAt).format("YYYYMMDD"));
console.log(updatedAt);

//test function
pm.test("Expect updatedAt to at least be createdAt date", () => {
    //assertion to check if updatedAt is >= createdAt
    pm.expect(updatedAt).to.be.at.least(createdAt);
});
1 Like

Thanks man! Life saver :slight_smile: