Date string conversion within an array

Hi!

I have a msg.payload.date with the value "8/8/22, 12:00 AM – 8/9/22, 12:00 AM"
I need this formatted as a timestamp for use with the moment node.

But I really can't work this out.
What is the best way to do this? I would prefere the output to be in a relative date

There are 2 dates there do you need both converting to a timestramp?
You know you can use moment in the change node with JSONata
e.g.

[{"id":"9fcc5f4e.1320e","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload.date","v":"8/8/22, 12:00 AM – 8/9/22, 12:00 AM","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":100,"y":1160,"wires":[["444ce64e.90464"]]},{"id":"444ce64e.90464","type":"change","z":"30af2d3e.d94ea2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"($dates := $split($$.payload.date, \" – \");\t[$moment($dates[0]).format(\"YYYY-MM-DD HH:mm:ss\"),\t$moment($dates[1]).format(\"YYYY-MM-DD HH:mm:ss\"),\t$toMillis($moment($dates[0]).format())]\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":1160,"wires":[["2dfdae9e.4eab8a"]]},{"id":"2dfdae9e.4eab8a","type":"debug","z":"30af2d3e.d94ea2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":1160,"wires":[]}]
1 Like

I might in some cases.

That did exactly what I wanted. :pray: Thanks!

I've spent hours reading here and there about date formatting with JSONata without understanding enough. Example was quite useful, and I also learned from it.

Did not know of JSONata until I started with Node-Red. I find it difficult.
Is there some kind of JSONata builder out there?

If you find JSONata difficult (many do, for more than very simple tasks) then don't use it. There is never a need to use it, javascript can always do the same thing. The javascript code will be longer but will be more efficient at run time. Also when you come back to tweak it in a years time you are more likely to understand what the js is doing. JSONata is great if your brain is wired so that you can get your head round it, but not so great for us lesser mortals.

Haha.. This part of my brain is more like a poor wireless :slight_smile:

I just did a JSONata string to number conversion in a switch node. That was easy enough tho.
In time, there propably will come more.
I agree, javascript is more worth the time

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