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

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

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:

{
      "---": {
        "---": 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:

  │ }
  │ {
  │   ---: { ---: 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?

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.

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"

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.

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?

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…

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.

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