Compare the date from a API key value in Postman



  • 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- 
    2019
     console.log(license_valid_till)
    
     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
     console.log(today_date)
    

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



  • pm.test("Check birthday greater than 1988-18-01", () => {
        let date,
            isoFormatDate,
            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
    
            pm.expect(isoFormatDate).to.be.above(a);
        }
    });
    

    Please use the above code snippet as a reference.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2