# Date in String which is transformed when included in another String

**URL:** <https://discourse.nodered.org/t/date-in-string-which-is-transformed-when-included-in-another-string/76759>\
**Category:** General\
**Tags:** function-node\
**Created:** [20 March 2023 15:12 UTC](https://discourse.nodered.org/t/date-in-string-which-is-transformed-when-included-in-another-string/76759 "2023-03-20T15:12:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [20 March 2023 15:12 UTC](https://discourse.nodered.org/t/date-in-string-which-is-transformed-when-included-in-another-string/76759/1 "2023-03-20T15:12:16Z")

</div>

Hi all,  
I want to retrieve a selection of data from a database managed by InfluxDB. The goal is to give a base date and add an interval (or subtract an interval ), to extract the data from this selection.  
Here is the flow:

```auto
[
    {
        "id": "12065710fa4a2085",
        "type": "debug",
        "z": "22872ad18901ecfc",
        "name": "out2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 850,
        "y": 4180,
        "wires": []
    },
    {
        "id": "3bd10a6e410ab1c9",
        "type": "function",
        "z": "22872ad18901ecfc",
        "name": "Query data",
        "func": "//for the example :\nflow.set(\"dataType\",\"phMesure\")\nflow.set(\"interval\", 3600000)//=1h\nflow.set(\"debut\", msg.payload)\nflow.set(\"fin\", msg.payload)\n\n//start program :\nlet dataType = flow.get(\"dataType\")\nlet finString\nlet debutString\nlet interval =flow.get(\"interval\")//3600000 //=1h\nlet debut = flow.get(\"debut\")//\nlet fin = flow.get(\"fin\")//\n//node.warn(debutString);\n\n//select data range\nif(msg.topic ==\"debut\"){//From Date + interval\n finString = millisToDate(debut+interval)//----------> Fonction\n debutString = millisToDate(debut)\n}\nif (msg.topic == \"fin\") {//From Date - interval\n debutString= millisToDate(fin - interval)//----------> Fonction\n finString = millisToDate(fin)\n}\n//for DEBUG :\nmsg.interval=interval\nmsg.debutString = debutString\nmsg.finString = finString\n\n//Send To InfluxDB node:\nmsg.query = `from(bucket: \"rlinkBucket\")\n |> range(start:`+ debutString + `,stop:` + finString + `)\n |> filter(fn: (r) => r[\"_measurement\"] == \"COMBO\")\n |> filter(fn: (r) => r[\"_field\"] == \"`+ dataType + `\")`;\n\nreturn msg;\n/* Tx msg.\npayload: 2023-03-20T11:08:33.838Z\ntopic: \"debut\"\ninterval: 3600000\ndebutString: \"2023-03-20T11:08:33.838Z\"\nfinString: \"2023-03-20T12:08:33.838Z\"\n*/\n/*Tx: msg.query\nfrom(bucket: \"rlinkBucket\")\n |> range(\n start:Mon Mar 20 2023 12:08:33 GMT+0100 (Central European Standard Time),\n stop:Mon Mar 20 2023 13:08:33 GMT+0100 (Central European Standard Time))\n |> filter(fn: (r) => r[\"_measurement\"] == \"COMBO\")\n |> filter(fn: (r) => r[\"_field\"] == \"phMesure\")\n*/\n\n\n////////// FONCTION ///////////\nfunction millisToDate(millis){\n let d = new Date(0);\n d.setUTCMilliseconds(millis);\n return d;\n}\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 600,
        "y": 4180,
        "wires": [
            [
                "12065710fa4a2085"
            ]
        ]
    },
    {
        "id": "2a686be307d08281",
        "type": "inject",
        "z": "22872ad18901ecfc",
        "name": "1679230860000 fin",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "fin",
        "payload": "1679310513838",
        "payloadType": "num",
        "x": 330,
        "y": 4170,
        "wires": [
            [
                "3bd10a6e410ab1c9"
            ]
        ]
    },
    {
        "id": "c82e1995ba61fca2",
        "type": "inject",
        "z": "22872ad18901ecfc",
        "name": "1679230860000 debut",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "debut",
        "payload": "1679310513838",
        "payloadType": "num",
        "x": 340,
        "y": 4140,
        "wires": [
            [
                "3bd10a6e410ab1c9"
            ]
        ]
    }
]

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ff4dddb986b0f75c673805ff46b93790aa533632.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1ecd9836a1cbb757c34f110a6b0261d55bbc63bb.png)  
In the msg.debutString "Start:" (same for finString "Stop:") we can see the start date in the form `"2023-03-20T11:08:33.838Z"`  
but when included in the msg.query it changes to "start: `Mon Tue 20 2023 12:08:33 GMT+0100 (Central European Standard Time)"` (Of course the InfluxDB node gives me an error! )

Why is the String modified? Do you have a solution ? THANKS

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 March 2023 15:48 UTC](https://discourse.nodered.org/t/date-in-string-which-is-transformed-when-included-in-another-string/76759/2 "2023-03-20T15:48:42Z")

</div>

The variables `debutString` and `finSting` are not strings, they are Dates. When you include a Date in the query or show it in the debug it will be converted to a string. The method used to do that conversion may be different in different situations. To generate an ISO string use `debutString.toISOString()`. Also I suggest changing the name of the variables.

In your function

```auto
function millisToDate(millis){
  let d = new Date(0);
  d.setUTCMilliseconds(millis);
  return d;
}

```

all you need is  
`return newDate(millis)`  
so in fact you probably don't need a function at all.

---

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [21 March 2023 12:46 UTC](https://discourse.nodered.org/t/date-in-string-which-is-transformed-when-included-in-another-string/76759/3 "2023-03-21T12:46:35Z")

</div>

For those who want to see the correction of the program:

```auto
//for the example :

flow.set("dataType","phMesure")
flow.set("interval", 3600000)//=1h
flow.set("debut", msg.payload)
flow.set("fin", msg.payload)

//start program :
let dataType = flow.get("dataType")
let finString //= flow.get("finString")
let debutString //= flow.get("debutString")
let interval =flow.get("interval")//3600000 //=1h
let debut = flow.get("debut")//
let fin = flow.get("fin")//
//node.warn(debutString);
//node.warn(finString);

//select data range
if(msg.topic =="debut"){//From Date + interval
  finString = new Date(debut + interval)
  debutString = new Date(debut )
  //node.warn(debutString);
//node.warn(finString);
}
if (msg.topic == "fin") {//From Date - interval
  finString = new Date(fin + interval)
  debutString = new Date(fin)
  //node.warn(debutString);
  //node.warn(finString);
}
if (msg.topic == "refresh") {//From Date - interval
  finString = new Date(fin )
  debutString = new Date(debut)
  //node.warn(debutString);
  //node.warn(finString);
}
//for DEBUG :
/*
msg.interval=interval
msg.debutString = debutString
msg.finString = finString
*/
//Send To InfluxDB node:
msg.query = `from(bucket: "rlinkBucket")
  |> range(start:`+ debutString.toISOString() + `,stop:` + finString.toISOString() + `)
  |> filter(fn: (r) => r["_measurement"] == "COMBO")
  |> filter(fn: (r) => r["_field"] == "`+ dataType + `")`;

return msg;

```

The result is :

```auto
from(bucket: "rlinkBucket")
  |> range(start:2023-03-21T12:39:23.581Z,stop:2023-03-22T12:39:23.581Z)
  |> filter(fn: (r) => r["_measurement"] == "COMBO")
  |> filter(fn: (r) => r["_field"] == "phMesure")

```

Nice , thank you @Colin

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 April 2023 12:46 UTC](https://discourse.nodered.org/t/date-in-string-which-is-transformed-when-included-in-another-string/76759/4 "2023-04-04T12:46:46Z")

</div>

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