# Date / Format in a script

**URL:** <https://discourse.nodered.org/t/date-format-in-a-script/72919>\
**Category:** General\
**Created:** [29 December 2022 21:04 UTC](https://discourse.nodered.org/t/date-format-in-a-script/72919 "2022-12-29T21:04:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lafayette](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@Lafayette](https://discourse.nodered.org/u/Lafayette)\
**Post date:** [29 December 2022 21:04 UTC](https://discourse.nodered.org/t/date-format-in-a-script/72919/1 "2022-12-29T21:04:38Z")

</div>

Hi,  
After an HTTP Request I get an array. In this array I've got an object (date) formated like this : 2022-12-09 and I would like to convert it like this 09/12/2022. If I use the Moment Node I know how to do that for one element of the array but I don't know how to use this node in a script to convert the whole array. Is it possible or do I need to write a script without the Moment Node and how ?  
Thx.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [29 December 2022 21:07 UTC](https://discourse.nodered.org/t/date-format-in-a-script/72919/2 "2022-12-29T21:07:34Z")

</div>

Could you run the message through a Split node, Moment and then Join it back together?

---

<div class="post-metadata">

**Author:** ![Lafayette](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@Lafayette](https://discourse.nodered.org/u/Lafayette)\
**Post date:** [29 December 2022 21:09 UTC](https://discourse.nodered.org/t/date-format-in-a-script/72919/3 "2022-12-29T21:09:14Z")

</div>

Unfortunaletly not ☹

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [29 December 2022 21:10 UTC](https://discourse.nodered.org/t/date-format-in-a-script/72919/4 "2022-12-29T21:10:28Z")

</div>

Can you copy and paste here an example of your data?

---

<div class="post-metadata">

**Author:** ![Lafayette](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@Lafayette](https://discourse.nodered.org/u/Lafayette)\
**Post date:** [29 December 2022 21:13 UTC](https://discourse.nodered.org/t/date-format-in-a-script/72919/5 "2022-12-29T21:13:16Z")

</div>

{"data\_id":"9701816","iso":"180","event\_id\_cnty":"DRC27730","event\_id\_no\_cnty":"27730"," **event\_date":"2022-12-09**","year":"2022","time\_precision":"1","event\_type":"Battles","sub\_event\_type":"Armed clash","actor1":"ADF: Allied Democratic Forces","assoc\_actor\_1":"","inter1":"2","actor2":"Military Forces of the Democratic Republic of Congo (2019-)","assoc\_actor\_2":"Military Forces of Uganda (1986-)","inter2":"1","interaction":"12","region":"Middle Africa","country":"Democratic Republic of Congo","admin1":"Nord-Kivu","admin2":"Beni","admin3":"Ruwenzori","location":"Bulongo","latitude":"0.3260","longitude":"29.6928","geo\_precision":"1","source":"Radio Okapi","source\_scale":"National","notes":"On 9 December 2022, ADF clashed with joint FARDC - UPDF forces in Bulongo (Ruwenzori, Beni, Nord-Kivu) whilst the group was attempting to assault the city center. Early warning of locals enabled fast reaction of military forces who pushed away the assailants. Casualties unknown.","fatalities":"0","timestamp":"1670859656","iso3":"COD"}

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [29 December 2022 21:17 UTC](https://discourse.nodered.org/t/date-format-in-a-script/72919/6 "2022-12-29T21:17:15Z")

</div>

That's not an array but an object.  
Is it just one element of an array of similar events?

You can use moment in a Change node, though I'm not familiar with the syntax. There is an example [here](https://discourse.nodered.org/t/using-moment-in-change-node-jsonata-test-results-in-an-error/37995/5)

So a split, a change node applying your reformat change to msg.payload.event\_date and a join should work.

---

<div class="post-metadata">

**Author:** ![Lafayette](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@Lafayette](https://discourse.nodered.org/u/Lafayette)\
**Post date:** [29 December 2022 21:24 UTC](https://discourse.nodered.org/t/date-format-in-a-script/72919/7 "2022-12-29T21:24:56Z")

</div>

Yes, I've got at least 500 objects per array.

---

<div class="post-metadata">

**Author:** ![Lafayette](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@Lafayette](https://discourse.nodered.org/u/Lafayette)\
**Post date:** [29 December 2022 21:39 UTC](https://discourse.nodered.org/t/date-format-in-a-script/72919/8 "2022-12-29T21:39:31Z")

</div>

I'm going to try that. Thx.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 December 2022 21:59 UTC](https://discourse.nodered.org/t/date-format-in-a-script/72919/9 "2022-12-29T21:59:13Z")

</div>

You can also use the JavaScript INTL library in node.js - so you can either reference the library in your settings.js globals section (and `get` a reference to it in your function) or, if you allow function nodes to use local libraries (see settings.js), you could add it direct to the function node and use it directly.

---

<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:** [27 February 2023 21:59 UTC](https://discourse.nodered.org/t/date-format-in-a-script/72919/10 "2023-02-27T21:59:23Z")

</div>

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