I have a scenerio where I need to compare a date from an API response with today's date and check whether it is less than or equal.

The API response will give me a JSON object and in that JSON the key " License" will contain a date in the format "13-Aug-2019",I need to compare this date with today's date and if today's date is greater than "13-Aug-2019" gives a fail in the test results .

Below is the code I wrote to get the date in the license string and today's date,

Var body = JSON.parse(response Body);
Var body date=body["license"]
//Sample license value is " license cs code1 version 13-aug- 
2018 cnts ......"
var words = bodydate.split(' ');
license_valid_till=words[4]; // This will get the string 13-aug- 

 Var ts = new Date();
 var part=ts.toDateString();
 var dpart = part.split(' ');
 today_date=dpart[2] + "-" +dpart[1] +"-"+ dpart[3];
 //This will get the string 12-aug-2019

Now my question is how can I compare this two values,any suggestions will be of great help?

1 Answer 1

pm.test("Check birthday greater than 1988-18-01", () => {
    let date,
        a = new Date('1988-01-18').getTime();

    for (i = 0; i < jsonData.content.length; i++) {

        date = jsonData.content[i].birthday;

        isoFormatDate = new Date(Date.parse(date)).getTime(); // Converting to integer from date format


Please use the above code snippet as a reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.