# Change a string type to Date type

**URL:** <https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066>\
**Category:** General\
**Created:** [11 June 2019 10:42 UTC](https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066 "2019-06-11T10:42:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kirakiira](https://avatars.discourse-cdn.com/v4/letter/k/b5ac83/32.png) [@Kirakiira](https://discourse.nodered.org/u/Kirakiira)\
**Post date:** [11 June 2019 10:42 UTC](https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066/1 "2019-06-11T10:42:52Z")

</div>

Hello all,

I am trying to calculate a time difference between 2 dates (that I extract from a csv file), so I need my dates to have the correct date format.

Here's their current format : dd/MM/yyyy HH:mm

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/03bf35d0d2d2cfcb66622234235b413c4ed13f9b.png)

So far I couldn't find the solution.  
This is my first forum post ever and I'm a node-red beginner, so I hope I'm doing it right ^^'

Thanks a lot in advance !

---

<div class="post-metadata">

**Author:** ![Nxito](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nxito/32/88293_2.png) [@Nxito](https://discourse.nodered.org/u/Nxito)\
**Post date:** [11 June 2019 11:40 UTC](https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066/2 "2019-06-11T11:40:06Z")

</div>

I hope this helps:

```auto
[{"id":"ed19354c.502338","type":"function","z":"d1fe89f0.772168","name":"Today!","func":"var Time = new Date();\n var Hour = Time.getHours();\n var minute = Time.getMinutes();\n var Sec = Time.getSeconds();\n var Day = Time.getUTCDate();\n var month = Time.getMonth()+1;\n var year = Time.getFullYear();\n if(minute<10)\n minute = \"0\" + minute;\n if(Sec<10)\n Sec = \"0\" + Sec;\n //escribiendo sobre el campo de texto la Hour actual \n \n var Today =(Day+\":\"+month+\":\"+year+\"\\n\"+Hour+\":\"+minute+\":\"+Sec);\n msg.payload=Today;\nreturn msg;","outputs":1,"noerr":0,"x":480,"y":380,"wires":[["e95d533f.dd369"]]},{"id":"e95d533f.dd369","type":"debug","z":"d1fe89f0.772168","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":380,"wires":[]},{"id":"2fd86060.c9b9a","type":"inject","z":"d1fe89f0.772168","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":320,"y":380,"wires":[["ed19354c.502338"]]}]

```

---

<div class="post-metadata">

**Author:** ![Kirakiira](https://avatars.discourse-cdn.com/v4/letter/k/b5ac83/32.png) [@Kirakiira](https://discourse.nodered.org/u/Kirakiira)\
**Post date:** [11 June 2019 11:53 UTC](https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066/3 "2019-06-11T11:53:20Z")

</div>

Thanks for your example, it helped me understand a bit more how the Date format works. Unfortunately I'm not dealing with the current time but with past times, so it doesn't seem like I can use the Date() function...

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [11 June 2019 12:03 UTC](https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066/4 "2019-06-11T12:03:37Z")

</div>

You can feed an old date into the javascript date function, a quick google will show you how

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 June 2019 12:10 UTC](https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066/5 "2019-06-11T12:10:14Z")

</div>

You could also use `node-red-contrib-moment`. Here is an example with two dates being inject in the same msg and two copies of the moment node changing the format:

 ![58%20AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/186d997ef1b554472288283c90800e48ff33c039.png)  
and here is the flow:

```auto
[{"id":"dc5af5ba.bb9b6","type":"inject","z":"89bb7ba2.1f442","name":"","topic":"","payload":"{\"date1\":\"01/01/2018 05:05\",\"date2\":\"04/01/2018 15:56\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":60,"wires":[["1683539b.69c5ac","aad6e4b1.1bc4a8"]]},{"id":"1683539b.69c5ac","type":"debug","z":"89bb7ba2.1f442","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":290,"y":120,"wires":[]},{"id":"aad6e4b1.1bc4a8","type":"moment","z":"89bb7ba2.1f442","name":"","topic":"","input":"payload.date1","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"","locale":"en_US","output":"payload.date1n","outputType":"msg","outTz":"America/New_York","x":320,"y":60,"wires":[["6ebdd87f.2614c8"]]},{"id":"6ebdd87f.2614c8","type":"moment","z":"89bb7ba2.1f442","name":"","topic":"","input":"payload.date2","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"","locale":"en_US","output":"payload.date2n","outputType":"msg","outTz":"America/New_York","x":560,"y":60,"wires":[["a6e3c74a.183b1"]]},{"id":"a6e3c74a.183b1","type":"debug","z":"89bb7ba2.1f442","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":60,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Kirakiira](https://avatars.discourse-cdn.com/v4/letter/k/b5ac83/32.png) [@Kirakiira](https://discourse.nodered.org/u/Kirakiira)\
**Post date:** [11 June 2019 12:33 UTC](https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066/6 "2019-06-11T12:33:57Z")

</div>

I had tried some of the solutions I found but it didn't work for me and I couldn't find out why

---

<div class="post-metadata">

**Author:** ![Kirakiira](https://avatars.discourse-cdn.com/v4/letter/k/b5ac83/32.png) [@Kirakiira](https://discourse.nodered.org/u/Kirakiira)\
**Post date:** [11 June 2019 12:36 UTC](https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066/7 "2019-06-11T12:36:12Z")

</div>

This is working perfectly !! Thanks a lot 😁

Hope this will help other people in the future !

Have a good day everyone

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [11 June 2019 12:37 UTC](https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066/8 "2019-06-11T12:37:00Z")

</div>

Then read the section here about logging events  
[https://nodered.org/docs/writing-functions#adding-status](https://nodered.org/docs/writing-functions#adding-status)

You can then add “debug lines” within your function  
to understand the different steps and understand more why it isn’t working

---

<div class="post-metadata">

**Author:** ![Kirakiira](https://avatars.discourse-cdn.com/v4/letter/k/b5ac83/32.png) [@Kirakiira](https://discourse.nodered.org/u/Kirakiira)\
**Post date:** [11 June 2019 12:38 UTC](https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066/9 "2019-06-11T12:38:17Z")

</div>

Thank you, I will take a look at that too when I have another issue ^^

---

<div class="post-metadata">

**Author:** ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)\
**Post date:** [21 May 2021 08:32 UTC](https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066/10 "2021-05-21T08:32:44Z")

</div>

Hello there, "April 24 2021 5:17 PM" How can I convert this format to date format?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 May 2021 08:39 UTC](https://discourse.nodered.org/t/change-a-string-type-to-date-type/12066/11 "2021-05-21T08:39:10Z")

</div>

If you mean convert the `string` "April 24 2021 5:17 PM" to a date object then...

in a function node, put...

```auto
msg.payload = new Date("April 24 2021 5:17 PM"); 

//or use msg.payload instead of a hardcoded datetime
//msg.payload = new Date(msg.payload); 

return msg;

```
