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

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