# Convert string date format

**URL:** <https://discourse.nodered.org/t/convert-string-date-format/39124>\
**Category:** General\
**Created:** [14 January 2021 14:59 UTC](https://discourse.nodered.org/t/convert-string-date-format/39124 "2021-01-14T14:59:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![leoborlot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leoborlot/32/31253_2.png) [@leoborlot](https://discourse.nodered.org/u/leoborlot)\
**Post date:** [14 January 2021 14:59 UTC](https://discourse.nodered.org/t/convert-string-date-format/39124/1 "2021-01-14T14:59:00Z")

</div>

Hi guys, thanks for every help then you give to me.

Now i need to solve this. I have a value sting date, I need to reformat it.  
the orignal --\> new format.  
06/11/2020 --\> 2020-11-06

How may I do it?

---

<div class="post-metadata">

**Author:** ![fellinga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fellinga/32/30631_2.png) [@fellinga](https://discourse.nodered.org/u/fellinga)\
**Post date:** [14 January 2021 15:38 UTC](https://discourse.nodered.org/t/convert-string-date-format/39124/2 "2021-01-14T15:38:07Z")

</div>

You can do something like this:

```auto
function padZero(i) {
    return i < 10 ? "0"+i : i;
}

const dateString = "06/11/2020";
const parts = dateString.split("/");
const date = new Date(Number(parts[2]), Number(parts[1]) - 1, Number(parts[0]));

const newDateString = date.getFullYear()+'-' + (padZero(date.getMonth()+1)) + '-'+padZero(date.getDate());

```

---

<div class="post-metadata">

**Author:** ![leoborlot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leoborlot/32/31253_2.png) [@leoborlot](https://discourse.nodered.org/u/leoborlot)\
**Post date:** [14 January 2021 15:41 UTC](https://discourse.nodered.org/t/convert-string-date-format/39124/3 "2021-01-14T15:41:36Z")

</div>

lets me try this!

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [14 January 2021 16:08 UTC](https://discourse.nodered.org/t/convert-string-date-format/39124/4 "2021-01-14T16:08:39Z")

</div>

Now is the question or the input date is US or rest of the world notation.  
Eq is 6 the month or 11 the month 😉

---

<div class="post-metadata">

**Author:** ![leoborlot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leoborlot/32/31253_2.png) [@leoborlot](https://discourse.nodered.org/u/leoborlot)\
**Post date:** [14 January 2021 16:28 UTC](https://discourse.nodered.org/t/convert-string-date-format/39124/5 "2021-01-14T16:28:22Z")

</div>

> [@fellinga](#):
>
> ```auto
> function padZero(i) {
> return i < 10 ? "0"+i : i;
> }
> 
> const dateString = "06/11/2020";
> const parts = dateString.split("/");
> const date = new Date(Number(parts[2]), Number(parts[1]) - 1, Number(parts[0]));
> 
> const newDateString = date.getFullYear()+'-' + (padZero(date.getMonth()+1)) + '-'+padZero(date.getDate());
> 
> ```

The "official" date is DD/MM/YYYY

---

<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:** [14 January 2021 16:56 UTC](https://discourse.nodered.org/t/convert-string-date-format/39124/6 "2021-01-14T16:56:55Z")

</div>

Alternatively this might do it for you, assuming it is coming in in msg.payload then  
`msg.payload = msg.payload.split("/").reverse().join("-")`

---

<div class="post-metadata">

**Author:** ![leoborlot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leoborlot/32/31253_2.png) [@leoborlot](https://discourse.nodered.org/u/leoborlot)\
**Post date:** [14 January 2021 17:03 UTC](https://discourse.nodered.org/t/convert-string-date-format/39124/7 "2021-01-14T17:03:14Z")

</div>

> [@Colin](#):
>
> msg.payload = msg.payload.split("/").reverse().join("-")

Ouuuwwww, very simple!!!  
It works, thanks a lot.

---

<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:** [15 March 2021 17:03 UTC](https://discourse.nodered.org/t/convert-string-date-format/39124/8 "2021-03-15T17:03:14Z")

</div>

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