Date in String which is transformed when included in another String

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:

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


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

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

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.

1 Like

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

//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 :

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

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