# Date string conversion within an array

**URL:** https://discourse.nodered.org/t/date-string-conversion-within-an-array/66085
**Category:** General
**Created:** [5 August 2022 16:53 UTC](https://discourse.nodered.org/t/date-string-conversion-within-an-array/66085 "2022-08-05T16:53:43Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![pat1](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@pat1](https://discourse.nodered.org/u/pat1)
#### Post date: [5 August 2022 16:53 UTC](https://discourse.nodered.org/t/date-string-conversion-within-an-array/66085/1 "2022-08-05T16:53:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [5 August 2022 18:43 UTC](https://discourse.nodered.org/t/date-string-conversion-within-an-array/66085/2 "2022-08-05T18:43:40Z")

</div>

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.

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

### Author: ![pat1](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@pat1](https://discourse.nodered.org/u/pat1)
#### Post date: [5 August 2022 21:03 UTC](https://discourse.nodered.org/t/date-string-conversion-within-an-array/66085/3 "2022-08-05T21:03:22Z")

</div>

> [@E1cid](#):
>
> There are 2 dates there do you need both converting to a timestramp?

I might in some cases.

> [@E1cid](#):
>
> You know you can use moment in the change node with JSONata  
> e.g.

That did exactly what I wanted. 🙏 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?

---

<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: [6 August 2022 07:47 UTC](https://discourse.nodered.org/t/date-string-conversion-within-an-array/66085/4 "2022-08-06T07:47:33Z")

</div>

> [@pat1](#):
>
> I find it difficult

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.

---

<div class="post-metadata">

### Author: ![pat1](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@pat1](https://discourse.nodered.org/u/pat1)
#### Post date: [7 August 2022 08:24 UTC](https://discourse.nodered.org/t/date-string-conversion-within-an-array/66085/5 "2022-08-07T08:24:23Z")

</div>

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

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

---

<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: [21 August 2022 08:25 UTC](https://discourse.nodered.org/t/date-string-conversion-within-an-array/66085/6 "2022-08-21T08:25:02Z")

</div>

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