# Convert format of datetime from database to local time with Moment node

**URL:** https://discourse.nodered.org/t/convert-format-of-datetime-from-database-to-local-time-with-moment-node/27649
**Category:** Dashboard
**Created:** [2 June 2020 02:49 UTC](https://discourse.nodered.org/t/convert-format-of-datetime-from-database-to-local-time-with-moment-node/27649 "2020-06-02T02:49:48Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jan-k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jan-k/32/17738_2.png) [@jan-k](https://discourse.nodered.org/u/jan-k)
#### Post date: [2 June 2020 02:49 UTC](https://discourse.nodered.org/t/convert-format-of-datetime-from-database-to-local-time-with-moment-node/27649/1 "2020-06-02T02:49:48Z")

</div>

Greetings,

I had tried to use the moment node to convert the datetime from a mysql database to local time and it works. Now I am trying to convert for an array of datetime with moment node. My database was storing the reading of temperature and humidity and the datetime. So, I make a dropdown for the user to choose the date and it will extract the data from database. I've tried a simple method to make a for loop and pushing each data to the moment node, but the output only one. Hope someone can help me to solve this problem, thank you in advance.

Best Regards.

My flow :  
`[{"id":"7f4187e7.18f708","type":"inject","z":"ecd7ee06.58772","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":700,"wires":[["649e33b6.df01fc"]]},{"id":"649e33b6.df01fc","type":"function","z":"ecd7ee06.58772","name":"","func":"msg.payload = [28.8,\"2020-05-27T08:48:37.000Z\",28.7,\"2020-05-27T08:48:38.000Z\",28.6,\"2020-05-27T08:48:39.000Z\",28.6,\"2020-05-27T09:14:37.000Z\"];\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":700,"wires":[["a3c16d89.78afb"]]},{"id":"b789b9ed.2c1ac8","type":"moment","z":"ecd7ee06.58772","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Oslo","adjAmount":0,"adjType":"days","adjDir":"add","format":"DD-MM-YYYY HH:mm:ss","locale":"en_US","output":"payload","outputType":"msg","outTz":"Asia/Kuala_Lumpur","x":740,"y":700,"wires":[["fd9995aa.4f9e28"]]},{"id":"a3c16d89.78afb","type":"function","z":"ecd7ee06.58772","name":"","func":"var arr=[];\nvar index = 1;\nvar myarr=msg.payload;\nfor (i = 0; i < myarr.length; i++)\n{\n msg.payload = myarr[index];\n index+=2;\n return msg;\n}\n","outputs":1,"noerr":0,"x":530,"y":700,"wires":[["b789b9ed.2c1ac8"]]},{"id":"fd9995aa.4f9e28","type":"debug","z":"ecd7ee06.58772","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":990,"y":700,"wires":[]}]`

---

<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: [2 June 2020 05:59 UTC](https://discourse.nodered.org/t/convert-format-of-datetime-from-database-to-local-time-with-moment-node/27649/2 "2020-06-02T05:59:51Z")

</div>

Can I ask why you need to do this? Often one would only do such conversion at the point of display.

---

<div class="post-metadata">

### Author: ![jan-k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jan-k/32/17738_2.png) [@jan-k](https://discourse.nodered.org/u/jan-k)
#### Post date: [2 June 2020 06:15 UTC](https://discourse.nodered.org/t/convert-format-of-datetime-from-database-to-local-time-with-moment-node/27649/3 "2020-06-02T06:15:21Z")

</div>

Yes, I want to show these data with a line chart. But because of the datetime format get from database to node red will be something like "2020-05-27T08:48:37.000Z" which is not the correct format for the chart.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [2 June 2020 07:05 UTC](https://discourse.nodered.org/t/convert-format-of-datetime-from-database-to-local-time-with-moment-node/27649/4 "2020-06-02T07:05:13Z")

</div>

That is standard ISO standard, you shouldnt need moment to convert that to millisecs epoch. The standard javascript date functions can do that.

---

<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: [2 June 2020 08:05 UTC](https://discourse.nodered.org/t/convert-format-of-datetime-from-database-to-local-time-with-moment-node/27649/5 "2020-06-02T08:05:12Z")

</div>

> [@jan-k](#):
>
> "2020-05-27T08:48:37.000Z"

You can create a javascript Date from that using the Date constructor and then use getTicks() to convert it to ticks for feeding to the chart . Something like this  
`msg.payload = new Date(msg.payload).getTime()`

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cd4319eea9d5572488cfd47bf0fbf33722fae0d9.png)

```auto
[{"id":"56b18f59.c97908","type":"inject","z":"bdd7be38.d3b55","name":"2020-05-27T08:48:37.000Z","topic":"","payload":"2020-05-27T08:48:37.000Z","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":158,"y":1403,"wires":[["2bed9c3d.5da2e4"]]},{"id":"2bed9c3d.5da2e4","type":"function","z":"bdd7be38.d3b55","name":"Create Ticks from payload","func":"msg.payload = new Date(msg.payload).getTime()\nreturn msg;","outputs":1,"noerr":0,"x":417,"y":1402,"wires":[["3eaa586e.b3a17"]]},{"id":"3eaa586e.b3a17","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":628,"y":1402,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![jan-k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jan-k/32/17738_2.png) [@jan-k](https://discourse.nodered.org/u/jan-k)
#### Post date: [3 June 2020 06:41 UTC](https://discourse.nodered.org/t/convert-format-of-datetime-from-database-to-local-time-with-moment-node/27649/6 "2020-06-03T06:41:37Z")

</div>

Wow great, this is simple and helpful. Thank you @Colin. 👍

---

<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: [17 June 2020 06:41 UTC](https://discourse.nodered.org/t/convert-format-of-datetime-from-database-to-local-time-with-moment-node/27649/7 "2020-06-17T06:41:49Z")

</div>

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