# Verify a date format from a post response

**URL:** <https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [February 27, 2020, 1:30am UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046 "2020-02-27T01:30:39Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisG9612](https://avatars.discourse-cdn.com/v4/letter/c/4da419/32.png) [@ChrisG9612](https://community.postman.com/u/ChrisG9612)\
**Post date:** [February 27, 2020, 1:30am UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046/1 "2020-02-27T01:30:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [February 27, 2020, 8:34am UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046/2 "2020-02-27T08:34:35Z")

</div>

Hey @ChrisG9612,

Welcome to the community! 🏆

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}$/);
});
```

---

<div class="post-metadata">

**Author:** ![ChrisG9612](https://avatars.discourse-cdn.com/v4/letter/c/4da419/32.png) [@ChrisG9612](https://community.postman.com/u/ChrisG9612)\
**Post date:** [February 27, 2020, 9:44am UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046/3 "2020-02-27T09:44:49Z")

</div>

> [@danny-dainton](#):
>
> chai `.match()` function

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

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [February 27, 2020, 10:12am UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046/4 "2020-02-27T10:12:34Z")

</div>

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:

> **[Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript](https://regex101.com/#javascript)**
>
> Regex101 allows you to create, debug, test and have your expressions explained for PHP, PCRE, Python, Golang and JavaScript. The website also features a community where you can share useful expressions.

---

<div class="post-metadata">

**Author:** ![aschultz](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@aschultz](https://community.postman.com/u/aschultz)\
**Post date:** [February 27, 2020, 2:51pm UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046/5 "2020-02-27T14:51:29Z")

</div>

Your pretty close with your first try. I recommend using moments isValid() function. [https://momentjs.com/docs/](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;
```

---

<div class="post-metadata">

**Author:** ![smrutiranjanir](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@smrutiranjanir](https://community.postman.com/u/smrutiranjanir)\
**Post date:** [October 1, 2020, 8:54pm UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046/6 "2020-10-01T20:54:20Z")

</div>

```auto
[
    {
        "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"
    }
]

```

```auto
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

---

<div class="post-metadata">

**Author:** ![harutharut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/harutharut/32/6996_2.png) [@harutharut](https://community.postman.com/u/harutharut)\
**Post date:** [March 11, 2021, 1:45pm UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046/7 "2021-03-11T13:45:06Z")

</div>

Hey @danny-dainton ,

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

How can I check this? Thanks

---

<div class="post-metadata">

**Author:** ![harutharut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/harutharut/32/6996_2.png) [@harutharut](https://community.postman.com/u/harutharut)\
**Post date:** [March 11, 2021, 1:46pm UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046/8 "2021-03-11T13:46:37Z")

</div>

/^\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

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 11, 2021, 2:23pm UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046/9 "2021-03-11T14:23:12Z")

</div>

Maybe something like this:

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

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 11, 2021, 2:31pm UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046/10 "2021-03-11T14:31:52Z")

</div>

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

I would go for something like:

```auto
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}/);
    })
});

```

---

<div class="post-metadata">

**Author:** ![harutharut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/harutharut/32/6996_2.png) [@harutharut](https://community.postman.com/u/harutharut)\
**Post date:** [March 11, 2021, 2:43pm UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046/11 "2021-03-11T14:43:04Z")

</div>

Thank you, it works.

---

<div class="post-metadata">

**Author:** ![gonsag](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gonsag](https://community.postman.com/u/gonsag)\
**Post date:** [November 28, 2023, 12:08am UTC](https://community.postman.com/t/verify-a-date-format-from-a-post-response/11046/12 "2023-11-28T00:08:25Z")

</div>

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.
