Verify a date format from a post response

Hello all, I have created a post request
which basically adds a user and an ID, when the response returns, it also passes back a date.

so response body looks like this.
From BODY

{
    "data": {
        "name": "Fred Dibner",
        "date": "27-02-2020 01:08:51"
    },
    "result": "32"
}

In the test, I have written

let moment = require ('moment');

pm.test("Date is present in correct format", function () {
    pm.expect(jsonData.data.date).to.include(moment().format('DD-MM-YYYY hh:mm:ss'));
});

However, whenever I run the test though the format returns correctly, I’m faced with the fact the seconds in the date/time different by 2 to 3 seconds and the test fails. I thought I was just asking for the format but it appears not. Does anyone know how I can just validate the date format regardless of what time the test ran or even was delayed by?

Mucho thanks in advance.

1 Like

Hey @ChrisG9612,

Welcome to the community! :trophy:

If you’re just wanting to check that the format is correct, you could use the chai .match() function. This takes regular expressions so you will be able to add something to match this pattern.

let jsonData = pm.response.json();

pm.test("Date is present in correct format", function () {
    pm.expect(jsonData.data.date).to.match(/^\d{2}-\d{2}-\d{4}\s\d{2}:\d{2}:\d{2}$/);
});

Thank you this works when the separator is a - however this seems to fail of the date separator is /
thus checking a format for dd/mm/yyyy hh:mm:ss fails

You would need to escape that character.

/^\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2}$/

This is a great site for testing regular expressions:

1 Like

Your pretty close with your first try. I recommend using moments isValid() function. https://momentjs.com/docs/

moment(date, format, locale, strict).isValid() will return a boolean. locale and strictness are optional, in this case you want to strict to verify the date is exact.

pm.expect(moment(jsonData.data.date, 'DD-MM-YYYY hh:mm:ss', true).isValid()).is.true;
1 Like
[
    {
        "totalQuestion": "30",
        "totalMark": "50",
        "difficultyCode": "Easy",
        "negativeMark": "0.00",
        "isActive": "1",
        "scheduledDateTime": "0002-11-30 00:00:00.0",
        "endDateTime": "0002-11-30 00:00:00.0",
        "testId": "1",
        "testName": "satya",
        "difficultyLevel": "1",
        "isScheduled": "1",
        "isNegativeMarking": "0"
    }
]
for (i = 0; i < jsonData.length; i++) {
    pm.test("Date is present in correct format", function () {
        pm.expect(jsonData[i].endDateTime).to.match(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}:\d{1}$/);
    });
}

kindly help me with this

Hey @danny-dainton ,

One question. I have this response
DateofBirth: “10-10-1981/^/11-02-1975”

How can I check this? Thanks

/^\d{2}-\d{2}-\d{4}//^//^\d{2}-\d{2}-\d{4}/
/^\d{2}-\d{2}-\d{4}/, /^\d{2}-\d{2}-\d{4}/

I checked these, but test case is failed

Maybe something like this:

pm.test("DoB Matches", () => {
    pm.expect(path.to.DateofBirth).to.match(/^\d{2}-\d{2}-\d{4}\/\^\/\d{2}-\d{2}-\d{4}$/);
});
1 Like

Totally missed this reply @smrutiranjanir - Is this still an issue?

I would go for something like:

pm.test("Date is present in correct format", () => {
    jsonData.forEach(obj => {
        pm.expect(obj.endDateTime).to.match(/\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}.\d{1}/);
    })
});

Thank you, it works.

1 Like

This site mentioned above is really helpful especially with the reference bar on the right menu to understand what expressions can be used plus now the debugger of the regex is an amazing feature. Highly recommend it. Thanks @danny-dainton for sharing this.

1 Like