Date to mysql datetime

I have date like this datetime = "2021-10-18T13:20:51.404Z"
I want like this "2021-10-18 13:20:51" for mysql database
and array = [20,30,40,50,60,70,40,30];
datetime is for array[0] (20)
I want something like this

o= {
array[0] : datetime,
array[1] : datetime + 30min,
array[2]: datetime +30min * 2,
array[3]: datetime + 30min * 3

Not really sure i understand what you require, so i have taken a stab/guess. If incorrect you may need to explain with more info and clearer examples.
try this

[{"id":"706be103.6180a8","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.[\t   [0,1,2,3].(\t       $v:=$;\t       $moment($$.date_time).add($v*30,\"minutes\").tz(\"UTC\").format(\"YYYY/MM/DD HH:mm:ss\")\t)\t]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":4680,"wires":[["60bab2b0.acbc64"]]},{"id":"18f08d85.db084a","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"date_time","v":"2021-10-18T13:20:51.404Z","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[20,30,40,50,60,70,40,30]","payloadType":"json","x":160,"y":4600,"wires":[["706be103.6180a8"]]},{"id":"60bab2b0.acbc64","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":4680,"wires":[]}]

I have error

I want something like this

What version of node-red are you on? earlier versions do not have access to momentjs.
please also supply a copyable array example of incoming data. as i have guessed what it is.
when you update your node-red this jsonata expression should get you the result you need.

$map($$.payload, function($v,$i){
       {$moment($$.date_time).add($i*30,"minutes").tz("UTC").format("YYYY/MM/DD HH:mm:ss"): $v}

[edit] This expression should work with older versions of node-red

               " "
           ): $v

I updated my node red and it works :slight_smile:
One questions
Where I can set my start date_time = "2021-10-18T13:20:51.404Z" in payload?

In my example it is in msg.date_time. msg.payload is already an array.
You do not need to use msg.payload you are free to use any other msg property you wish.

1 Like

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