# Help with message formatting

**URL:** <https://discourse.nodered.org/t/help-with-message-formatting/64057>\
**Category:** General\
**Created:** [17 June 2022 09:08 UTC](https://discourse.nodered.org/t/help-with-message-formatting/64057 "2022-06-17T09:08:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 June 2022 09:08 UTC](https://discourse.nodered.org/t/help-with-message-formatting/64057/1 "2022-06-17T09:08:53Z")

</div>

Today's problem for me....

I have the `msg.payload` loaded with the time as an array (I think that is the right term).  
So (example)  
`msg.payload.0` is the seconds.  
`msg.payload.1` is the minutes.  
`msg.payload.2` is the hour.  
and so on.

_Problem_  
I would prefer the values are _formatted_ with 2 digits.  
So if values are less than `10` there is a leading `0`.

This is what I have to create the message:

```auto
[{"id":"314fee2e910159cc","type":"template","z":"0918ee609bf69fc7","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.7}}/{{payload.6}}/{{payload.5}} {{payload.2}}:{{payload.1}}:{{payload.0}}","output":"str","x":4570,"y":590,"wires":[["d1f86109c5af8059"]]}]

```

Rather basic, yes.

I have never really got the `C++` command worked out and so doing it in Javascript is asking a bit much. 😉

Someone - please.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [17 June 2022 11:37 UTC](https://discourse.nodered.org/t/help-with-message-formatting/64057/2 "2022-06-17T11:37:56Z")

</div>

Can you post an actual msg.payload please ? Also do you want to have 2 digits in the 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:** [17 June 2022 11:56 UTC](https://discourse.nodered.org/t/help-with-message-formatting/64057/3 "2022-06-17T11:56:08Z")

</div>

you could use JSONata in a change node and $formatNumber()  
e.g.

```auto
[{"id":"2922ea64.0bdab6","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[1,2,3,4,5,6,7,8,9]","payloadType":"json","x":210,"y":3340,"wires":[["35c2b34b.4e1d94"]]},{"id":"35c2b34b.4e1d94","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.$formatNumber($,\"00\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":3340,"wires":[["314fee2e910159cc"]]},{"id":"314fee2e910159cc","type":"template","z":"bf9e1e33.030598","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.7}}/{{payload.6}}/{{payload.5}} {{payload.2}}:{{payload.1}}:{{payload.0}}","output":"str","x":330,"y":3420,"wires":[["ea62070d.c1a55"]]},{"id":"ea62070d.c1a55","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":3260,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 June 2022 21:27 UTC](https://discourse.nodered.org/t/help-with-message-formatting/64057/4 "2022-06-17T21:27:00Z")

</div>

`[34,26,7,0,6,18,6,2022]`

Is what I get from the NTP node

---

<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:** [1 July 2022 21:27 UTC](https://discourse.nodered.org/t/help-with-message-formatting/64057/5 "2022-07-01T21:27:37Z")

</div>

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