Compare the date in JSON response with the date(different format) set in environment variable

Hi All/ @danny-dainton

I have to write a test that compares the date in my JSON response in “2021-04-01T00:00:00” format with the date I have defined in environment variable(MM/DD/YYYY). I have seen some posts suggesting to use moment function to compare with current date but I am not able to use it for my situation and I tried lot of a solutions from different posts but none of them seemed logical(for me) for my scenario so I really need some help. Thank you all in advance.

I’ve already tried:

Can you paste the script code rather than a picture of it, please?

Can you also share an example of the response body?

Sure. Thank you
Here is the script code:

//* Validation that GET method is run successfully returning the status code as 200*//

pm.test(" LEP Service is accessible(200OK)", function () {
    pm.response.to.have.status(200);
});

pm.test("Service includes the necessary fields in response successfully", function () {
  pm.expect(pm.response.text()).to.include("SUBSCRIBER_ID");
  pm.expect(pm.response.text()).to.include("ELIGIBILITY_MONTH");
  pm.expect(pm.response.text()).to.include("LEP_AMOUNT");
  pm.expect(pm.response.text()).to.include("BEGIN_DATE");
  pm.expect(pm.response.text()).to.include("END_DATE");
  });

pm.test("Validating the accuracy of service output", () =>{
   
   const reponseJson = pm.response.json();

  let subscriberId=pm.environment.get("ValidSubID");
  pm.expect(reponseJson.SUBSCRIBER_ID).to.eql(subscriberId);
  
  let eligibilityMonth=pm.environment.get("Valideligibilitymonth");
  let responseeligibilitymonth=reponseJson.ELIGIBILITY_MONTH;
  // const moment = require('moment');
   //pm.environment.set(responseeligibilitymonth, moment().format("YYYY-MM-DD"));
  pm.expect((reponseJson.ELIGIBILITY_MONTH)).to.eql(eligibilityMonth);
});

And this is the response:

{
    "SUBSCRIBER_ID": "",
    "ELIGIBILITY_MONTH": "2021-04-01T00:00:00",
    "LEP_AMOUNT": 10,
    "BEGIN_DATE": "2021-01-01T00:00:00",
    "END_DATE": null
}

This is just an example based on some of your code and the response body. I’m using moment to change the format of the value in the response body, to match the format of the eligibilityMonth variable.

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


pm.test("Validating the accuracy of service output", () => {
    
    let eligibilityMonth = '01/04/2021';
    pm.expect(moment(responseJson.ELIGIBILITY_MONTH).format('DD/MM/YYYY')).to.eql(eligibilityMonth);
});
1 Like

@danny-dainton Thank you very much for the quick response - That worked. This is the part I was stuck with- moment(responseJson.ELIGIBILITY_MONTH).

1 Like