# 'Invalid Date' value in request if it generated by Prescript

**URL:** <https://community.postman.com/t/invalid-date-value-in-request-if-it-generated-by-prescript/53529>\
**Category:** Help Hub\
**Tags:** newman-project, variables, collections\
**Created:** [October 20, 2023, 11:44am UTC](https://community.postman.com/t/invalid-date-value-in-request-if-it-generated-by-prescript/53529 "2023-10-20T11:44:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aerospace-geologist3](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@aerospace-geologist3](https://community.postman.com/u/aerospace-geologist3)\
**Post date:** [October 20, 2023, 11:44am UTC](https://community.postman.com/t/invalid-date-value-in-request-if-it-generated-by-prescript/53529/1 "2023-10-20T11:44:37Z")

</div>

Hi All!  
I have the following pre-request script:

```auto
let today = pm.collectionVariables.get("currentDate");
let newdate = new Date(today);
pm.variables.set("startDay", new Date(newdate.setDate(28)).toLocaleDateString('en-US'));

```

It returns date in the format: 10/28/2023  
Further this date use in the request:

```auto
{
      "---": {
        "---": 2,
        "Name": "---",
        "---": true
      },
      "---": {
        "---": false,
        "CreatedDate": "{{startDay}}",
        "---": 1,
        "---": {
          "---": 1,
          "---": 2
        },
        "Points": [
          "{{startDay}} 12:00"
        ]
      }
}

```

It works fine when I run test manually or run collection in the Postman.  
But, when I ran the same in Newman, I got the following:

```auto
  │ }
  │ {
  │ ---: { ---: 2, ---: '---', ---
  │ : true },
  │ ---: {
  │ ---: false,
  │ CreatedDate: 'Invalid Date',
  │ ---: 1,
  │ ---: { ---: 1, ---: 2 },
  │ Points: ['Invalid Date 12:00']
  │ }
  │ }

```

Of course all tests fail.  
What could be wrong?  
Maybe it needs to be formatted somehow for the Newman?

---

<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:** [October 20, 2023, 4:53pm UTC](https://community.postman.com/t/invalid-date-value-in-request-if-it-generated-by-prescript/53529/2 "2023-10-20T16:53:42Z")

</div>

Does your value for the collection variable “currentDate” exist in the collection.JSON you export to run in Newman?

Please remember, that only initial values are exported.

What are you trying to do with that code? Is currentDate today?

I suspect its probably because of how to chain new Date() and toLocaleDateString together. (I had to Google it).

The following seems to work.

```auto
let startDate = new Date(new Date().setDate(28)).toLocaleDateString('en-us');
pm.collectionVariables.set("startDate", startDate)
console.log(pm.collectionVariables.get("startDate")); // "10/28/2023"

```

---

<div class="post-metadata">

**Author:** ![aerospace-geologist3](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@aerospace-geologist3](https://community.postman.com/u/aerospace-geologist3)\
**Post date:** [October 23, 2023, 8:36am UTC](https://community.postman.com/t/invalid-date-value-in-request-if-it-generated-by-prescript/53529/3 "2023-10-23T08:36:58Z")

</div>

Yes, “currentDate” exists in JSON format.  
Basic test logic:  
There is a base date - “baseDate”. This is a date in the future.  
Next, a certain test package is executed with this date.  
After this, the date is increased by 1 day and the tests are run again. The variable “currentDate” gets the current day.  
This is an emulation of the operation of services in the future several months in advance.

In this particular case, the variable “startDate” is needed for one of the services to generate the correct request in the future. This variable is local and generated in the pre-script and then passed to the body of the request.

---

<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:** [October 23, 2023, 8:40am UTC](https://community.postman.com/t/invalid-date-value-in-request-if-it-generated-by-prescript/53529/4 "2023-10-23T08:40:17Z")

</div>

> [@aerospace-geologist3](#):
>
> `"{{startDay}}"`

Should your variable be wrapped in quotes?

---

<div class="post-metadata">

**Author:** ![aerospace-geologist3](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@aerospace-geologist3](https://community.postman.com/u/aerospace-geologist3)\
**Post date:** [October 24, 2023, 10:37am UTC](https://community.postman.com/t/invalid-date-value-in-request-if-it-generated-by-prescript/53529/5 "2023-10-24T10:37:47Z")

</div>

> [@michaelderekjones](#):
>
> Should your variable be wrapped in quotes?

Yes, sure.  
This is how the variable is passed to the body of the request. The variable is of type string, so it is in quotes.  
What other options are there?

---

<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:** [October 24, 2023, 11:01am UTC](https://community.postman.com/t/invalid-date-value-in-request-if-it-generated-by-prescript/53529/6 "2023-10-24T11:01:05Z")

</div>

No, if you wrap the variable in quotes, it will be treated as the value.

If the variable is of type string, you don’t need to wrap it in quotes when you reference it.

Consider the following…

```auto
let startDate = new Date(new Date().setDate(28)).toLocaleDateString('en-us');

console.log(startDate); // "10/28/2023"
console.log("startDate"); // "startDate"

```

When you look at the console log, you will see the date is actually a string.

---

<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:** [November 23, 2023, 11:01am UTC](https://community.postman.com/t/invalid-date-value-in-request-if-it-generated-by-prescript/53529/7 "2023-11-23T11:01:08Z")

</div>

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