# Check that each datetime in array is between specified datetimes

**URL:** <https://community.postman.com/t/check-that-each-datetime-in-array-is-between-specified-datetimes/68010>\
**Category:** Help Hub\
**Tags:** tests, scripts, responseassertion\
**Created:** [September 4, 2024, 5:48pm UTC](https://community.postman.com/t/check-that-each-datetime-in-array-is-between-specified-datetimes/68010 "2024-09-04T17:48:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bethhalstead-meijer](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bethhalstead-meijer](https://community.postman.com/u/bethhalstead-meijer)\
**Post date:** [September 4, 2024, 5:48pm UTC](https://community.postman.com/t/check-that-each-datetime-in-array-is-between-specified-datetimes/68010/1 "2024-09-04T17:48:58Z")

</div>

I am calling an endpoint using before and after timestamp parameters like this:  
`{{baseUrl}}/activity?before=2024-09-04T13:55:02.325Z&after=2024-09-03T12:34:47.830Z`

Response body would be served as an array like this:

```auto
[
    {
        "eventType": "account-created",
        "invokedAt": "2024-09-04T13:54:52.153Z"
    },
    {
        "eventType": "account-preference-updated",
        "invokedAt": "2024-09-04T13:54:56.231Z",
        "changes": [
            {
                "property": "values",
                "prior": "",
                "new": "newValue"
            }
        ]
    },
    {
        "eventType": "account-preference-updated",
        "invokedAt": "2024-09-04T13:58:34.342Z",
        "changes": [
            {
                "property": "values",
                "prior": "newValue",
                "new": "newerValue"
            }
        ]
    }
]

```

How can I write a test that will evaluate whether the `invokedAt` timestamps are between the specified params? I will be using variables for the params to make it easy to comapre within the test.

The [closest solution](https://community.postman.com/t/new-to-postman-and-need-to-validate-date-range-returned-is-within-specified-range/20686) I have found was never fully answered, so I am not sure whether there could be an easily utilized Postman feature for it.

This is what I tried to build from that and some other various answers:

```auto
//Validate that each invokedAt timestamp is between the endpoint param timestamps
jsonData.forEach((obj, i) => {
    let invokedTime = obj.invokedAt;
    console.log(invokedTime);
    pm.test(`Object ${i} invoked time [${invokedTime}] within range `, () => {
        pm.expect((invokedTime).isBetween("2024-09-03T12:34:47.830Z", "2024-09-04T13:55:02.325Z")).to.be.true;
    })
});

```

It fails with error:  
`Object 1 invokedTime[2024-09-04T13:54:56.231Z] between | TypeError: invokedTime.isBetween is not a function`

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 5, 2024, 3:43pm UTC](https://community.postman.com/t/check-that-each-datetime-in-array-is-between-specified-datetimes/68010/2 "2024-09-05T15:43:12Z")

</div>

`isBetween` is not a valid Chai assertion so the error is correctly reporting that its not a function.

> **[Expect / Should - Chai](https://www.chaijs.com/api/bdd/)**
>
> Chai is a BDD / TDD assertion library for \[node\](http://nodejs.org) and the browser that can be delightfully paired with any javascript testing framework.

Format it using moment, and then parse it to an integer so it then becomes a simple number comparison.

```auto
var response = pm.response.json();

var moment = require('moment');

let begin=parseInt(moment("2024-09-03T12:34:47.830Z").format("YYYYMMDDHHmmss"));
let end=parseInt(moment("2024-09-04T13:55:02.325Z").format("YYYYMMDDHHmmss"));

response.forEach((obj, i) => {
    let invokedTime = parseInt(moment(obj.invokedAt).format("YYYYMMDDHHmmss"));
    pm.test(`Object ${i} invoked time [${invokedTime}] within range `, () => {
        pm.expect(invokedTime).to.be.within(begin, end);
    })
});

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/4/b4bdef37ecfc7dc1a68ce8c3c507fba7b7e86de0.png)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [October 5, 2024, 3:43pm UTC](https://community.postman.com/t/check-that-each-datetime-in-array-is-between-specified-datetimes/68010/3 "2024-10-05T15:43:21Z")

</div>

Closed on October 5, 2024, 3:43pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
