How to verify if ISO date on a endDate endPoint is not expired

Hi guys,
Could you please help me to validate that the endDate of promotions is not expired on the nested array.
This is a response where I need to check the endDate endPoint

{
    "data": {
        "findManyCommunities": {
            "totalCount": 3,
            "edges": [
                {
                    "node": {
                        "name": "Atria West 86",
                        "promotions": [
                            {
                                "startDate": "2022-09-01T19:00:00.000Z",
                                "endDate": "2023-01-06T20:00:00.000Z"
                            }
                        ]
                    }
                },
                {
                    "node": {
                        "name": "The Arbors at Hauppauge",
                        "promotions": [
                            {
                                "startDate": "2022-09-01T19:00:00.000Z",
                                "endDate": "2022-10-01T19:00:00.000Z"
                            },
                            {
                                "startDate": "2022-09-01T19:00:00.000Z",
                                "endDate": "2023-01-14T20:00:00.000Z"
                            }
                        ]
                    }
                },
                {
                    "node": {
                        "name": "Atria Forest Hills",
                        "promotions": [
                            {
                                "startDate": "2022-09-02T19:00:00.000Z",
                                "endDate": "2022-12-09T20:00:00.000Z"
                            },
                            {
                                "startDate": "2022-09-01T19:00:00.000Z",
                                "endDate": "2022-12-01T20:00:00.000Z"
                            },
                            {
                                "startDate": "2022-09-02T19:00:00.000Z",
                                "endDate": "2023-01-07T20:00:00.000Z"
                            }
                        ]
                    }
                }
            ]
        }
    }
} 

I attached a console log screenshot and this is my test that is not working…

const response=pm.response.json();
let moment = require ('moment');

console.log(response);
pm.test("Check that endDate is not expired", function () {
pm.expect(moment(response.data.findManyCommunities.edges[0].node.promotions[0].endDate, 'DD-MM-YYYY hh:mm:ss', true).isValid()).is.true;
});

Could you please help

1 Like

Consider the following. (I’m just working with the date, you will have to extend the code if you want the time stamp).

You need the date format to be consistent to do the assertion. So first step is to create two variables with the same format for the comparison.
Moment also creates a string, so you need to parse it to a number.

I’ve set the format to YYYYMMDD as its just a number comparison then. Pretty sure you can do date comparison if you format the variable as a date object correctly.

let moment = require ('moment');

var endDate = parseInt(moment(json.data.findManyCommunities.edges[0].node.promotions[0].endDate).format("YYYYMMDD"));
var todaysDate = parseInt((moment(new Date())).format("YYYYMMDD"));

// what are we working with - needs to be a number
console.log(typeof(endDate)); // number
console.log(typeof(todaysDate)); // number

console.log(endDate);
console.log(todaysDate);

pm.test("Expect endDate to at least todays date", () => {
    pm.expect(endDate).to.be.at.least(todaysDate);
});    

var futureDate = parseInt("20240106");
console.log(futureDate);
console.log(typeof(futureDate));

// same test but make it  fail
pm.test("Still expect endDate to at least future date", () => {
    pm.expect(endDate).to.be.at.least(futureDate);
}); 

image

1 Like

@michaelderekjones Hi Mike, Thank you so much!
You helped a lot and thank you for the clarifications in comments!!!