Skip to content
Advertisement

How to compare a string to a date in postman test?

Suppose a API request fetches a users id, email address and birthday. Sample API Request below:

JavaScript

For the above request, the following is the response:

JavaScript

Now, what will be the test in postman to make sure that all the returned values under birthday node is greater than 1988-18-01?

I have tried the following:

JavaScript

But postman says: “Check birthday greater than 1988-18-01 | AssertionError: expected ‘1990-01-01’ to be a number or a date”.

Advertisement

Answer

So firstly, the dates need to be converted to a format that JS accepts and use the Date constructor to generate the complete date.

Next, the ‘above’ function in pm accepts an integer, so the date format will not be compared. To fix this, we can convert the date to integer format by using the .getTime() function.

Lastly, it’s not a good practice to declare variables inside a for loop. Here’s what you can replace your test with:

JavaScript
Advertisement