How to get array using split node?

Hi,
I have a date time array, I use moment node to convert the format and store the new value in the resulting array called arrLastModDate.

I use the split node to split each element of the original date array to convert the format and the resulting array is also split :frowning:

[{"id":"20258e8b.666432","type":"function","z":"42ca3d14.2c3124","name":"Get array date time","func":"var arrLastModDate = [];\n\narrLastModDate.push('2020-04-07T10:00:56.000Z');\narrLastModDate.push('2020-04-07T09:45:32.000Z');\narrLastModDate.push('2020-04-07T09:47:04.000Z');\narrLastModDate.push('2020-04-07T09:48:54.000Z');\narrLastModDate.push('2020-04-07T09:50:28.000Z');\n\nmsg.payload = arrLastModDate;\n\nreturn msg;","outputs":1,"noerr":0,"x":1350,"y":1940,"wires":[["3f8b4fa1.f7dbb"]]},{"id":"c45bf03c.6b6be","type":"inject","z":"42ca3d14.2c3124","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1190,"y":1940,"wires":[["20258e8b.666432"]]},{"id":"2b4b7b9f.3fa2c4","type":"split","z":"42ca3d14.2c3124","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":1310,"y":2020,"wires":[["810bc72c.16e728"]]},{"id":"3f8b4fa1.f7dbb","type":"change","z":"42ca3d14.2c3124","name":"","rules":[{"t":"set","p":"arrUpdateDate","pt":"msg","to":"[]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1570,"y":1940,"wires":[["2b4b7b9f.3fa2c4"]]},{"id":"810bc72c.16e728","type":"moment","z":"42ca3d14.2c3124","name":"Convert format date time","topic":"","input":"payload","inputType":"msg","inTz":"Asia/Tokyo","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm:ss","locale":"ja_JP","output":"payload","outputType":"msg","outTz":"Asia/Tokyo","x":1490,"y":2020,"wires":[["153bdf58.60d591"]]},{"id":"153bdf58.60d591","type":"change","z":"42ca3d14.2c3124","name":"get msg.arrUpdateDate","rules":[{"t":"set","p":"arrUpdateDate","pt":"msg","to":"$append(arrUpdateDate, [payload])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1730,"y":2020,"wires":[["8224802c.a7801"]]},{"id":"8224802c.a7801","type":"debug","z":"42ca3d14.2c3124","name":"arrUpdateDate","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"arrUpdateDate","targetType":"msg","x":1740,"y":2100,"wires":[]}]

Can I use the split node? Or do i use a loop to convert the date format of each element of the original array?
Thanks!

You can convert the timestamps in the change node using jsonata and moment function. e.g.

[{"id":"8d63e49.f8f8198","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload","v":"","vt":"str"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":70,"y":1800,"wires":[["13c1586b.5c3538"]]},{"id":"13c1586b.5c3538","type":"function","z":"8d22ae29.7df6d","name":"Get array date time","func":"var arrLastModDate = [];\n\narrLastModDate.push('2020-04-07T10:00:56.000Z');\narrLastModDate.push('2020-04-07T09:45:32.000Z');\narrLastModDate.push('2020-04-07T09:47:04.000Z');\narrLastModDate.push('2020-04-07T09:48:54.000Z');\narrLastModDate.push('2020-04-07T09:50:28.000Z');\n\nmsg.payload = arrLastModDate;\n\nreturn msg;","outputs":1,"noerr":0,"x":230,"y":1800,"wires":[["3bb419f.4559a66"]]},{"id":"3bb419f.4559a66","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"arrUpdateDate","pt":"msg","to":"$map(payload, function($v){\t$moment($v).tz(\"Asia/Tokyo\").format('YYYY-MM-DD HH:mm:ss') \t})","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":1800,"wires":[["49e75bf9.5de18c"]]},{"id":"49e75bf9.5de18c","type":"debug","z":"8d22ae29.7df6d","name":"arrUpdateDate","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"arrUpdateDate","targetType":"msg","x":630,"y":1860,"wires":[]}]
1 Like

You can add a join node after the moment node.

1 Like

@E1cid Thank you for your support!
I ran it, but somehow got the error "Invalid JSONata expression: Attempted to invoke a non-function" :frowning:

@Steve-Mcl Thank you for your support!
I forgot that there is a join node @@

Strange this is what it returns for me

[{"id":"8d63e49.f8f8198","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload","v":"","vt":"str"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":70,"y":1800,"wires":[["13c1586b.5c3538"]]},{"id":"13c1586b.5c3538","type":"function","z":"8d22ae29.7df6d","name":"Get array date time","func":"var arrLastModDate = [];\n\narrLastModDate.push('2020-04-07T10:00:56.000Z');\narrLastModDate.push('2020-04-07T09:45:32.000Z');\narrLastModDate.push('2020-04-07T09:47:04.000Z');\narrLastModDate.push('2020-04-07T09:48:54.000Z');\narrLastModDate.push('2020-04-07T09:50:28.000Z');\n\nmsg.payload = arrLastModDate;\n\nreturn msg;","outputs":1,"noerr":0,"x":230,"y":1800,"wires":[["3bb419f.4559a66"]]},{"id":"3bb419f.4559a66","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"arrUpdateDate","pt":"msg","to":"$map(payload, function($v){\t$moment($v).tz(\"Asia/Tokyo\").format('YYYY-MM-DD HH:mm:ss') \t})","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":1800,"wires":[["49e75bf9.5de18c"]]},{"id":"49e75bf9.5de18c","type":"debug","z":"8d22ae29.7df6d","name":"arrUpdateDate","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"arrUpdateDate","targetType":"msg","x":630,"y":1860,"wires":[]}]

What is your node-red version?

1 Like

This is my node-red version:
Node-RED version: v0.20.7
Node.js version: v12.7.0

Ok thats the issue moments works from 1.1.
Maybe you should update your node-red.

1 Like

I got it.
Thank you again!

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