New to Postman and need to validate date range returned is within specified range

I’m testing an api that specifies a date range (i.e. 2021-01-01 to 2021-02-28). Is there a way to validate that the date field returned is within that range? I’ve looked at the moment variable but not sure if that will work in this case. I’m also not too familiar with JavaScript but trying to learn.
Thanks for your help

Hello @djoyce21 Warm welcome to the community :bouquet:

So I believe you need to verify if the specific date from the response in between the hardcoded dates given above.

If so, parse your response and save the date into a variable, if its in expected format you can save it directly and use. Else change the format and store it.

And then you can try the below snippet:

var date = new Date("2021-03-02");
var from = new Date("2021-01-01");
var to = new Date("2021-02-28");
pm.test("Dates within range", () => {
if (from <= date && date <= to) {
        console.log('Date is in Range');
console.log('Date is not in Range');'Dates not in Range');

Here I have defined all the three dates. Based on your expectation, tweak them please.

Please let me know if you have any troubles further.

Hi bpricilla, thank you for your help. so I created the following and need to do a for loop for it to verify the date for the number of records returned. When I run the code below, I get a Syntax Error: Unexpected token ‘)’". Am I putting the for loop in the correct place and do I need anything else? Again, thank your for your help.

//parse the trade date from the response


var jsondata = JSON.parse(responseBody);


//for (var i = 0; i < data.length; i++) {

//console.log(“TradeDate=[” + jsondata).TradeDate + “]”;

var tdate = jsondata.TradeDate

var fromdate = new Date(“2021-01-01”);

var todate = new Date(“2021-02-28”);

for (var i = 0; i < data.length; i++) {

pm.test(“TradeDates within range”, () => {

if (fromdate <= tdate && tdate <= todate) {

    console.log('TradeDate is in Range');



console.log(‘TradeDate is not in Range’);‘TradesDates not in Range’);



@djoyce21 Can you share the response please?

Unfortunately I won’t be able to share that info. But basically there are about 100 different ids returned. Each Id has 4 data fields one including the date field that I want to validate is in the range I’ve set