# Change date format

**URL:** <https://discourse.nodered.org/t/change-date-format/76212>\
**Category:** General\
**Created:** [7 March 2023 06:00 UTC](https://discourse.nodered.org/t/change-date-format/76212 "2023-03-07T06:00:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dineshaj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dineshaj/32/53342_2.png) [@dineshaj](https://discourse.nodered.org/u/dineshaj)\
**Post date:** [7 March 2023 06:00 UTC](https://discourse.nodered.org/t/change-date-format/76212/1 "2023-03-07T06:00:27Z")

</div>

Hi,

I have this function

```auto
let day = new Date("2005-03-01"); //YYYY-MM-DD
var end = new Date("2005-03-03");

while(day <= end){
    startDay = day.setDate(day.getDate() - 1);
    endDay = day.setDate(day.getDate() + 1);
    msg.payload= {
                    "startDay": startDay,
                    "endDay": endDay
                }
    node.send(msg);

    day = new Date( day.setDate(day.getDate() + 1) )
}

```

It outputs the following

![Screenshot 2023-03-07 112843](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/87941f0754951ea2511d6e39e22e954fa7e63d27.png)

how would it be possible to get the dates in DD-MM-YYYY format?

---

<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:** [7 March 2023 07:43 UTC](https://discourse.nodered.org/t/change-date-format/76212/2 "2023-03-07T07:43:00Z")

</div>

Where is it that you want to display the date in that format?

---

<div class="post-metadata">

**Author:** ![dineshaj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dineshaj/32/53342_2.png) [@dineshaj](https://discourse.nodered.org/u/dineshaj)\
**Post date:** [7 March 2023 07:58 UTC](https://discourse.nodered.org/t/change-date-format/76212/3 "2023-03-07T07:58:20Z")

</div>

The startDay and endDay will need to go to a post request to a URL

```auto
{
    "userId": 6412,
    "startDay": "01-01-2023",
    "endDay": "02-01-2023"
}

```

---

<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:** [7 March 2023 09:54 UTC](https://discourse.nodered.org/t/change-date-format/76212/4 "2023-03-07T09:54:34Z")

</div>

OK, I believe you can use node-red-contrib-moment to do such things. Or you could do it in javascript using the Date functions to get the individual parts and build the string.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [7 March 2023 20:37 UTC](https://discourse.nodered.org/t/change-date-format/76212/5 "2023-03-07T20:37:33Z")

</div>

This will work

```auto

let date = new Date(startDay).toISOString().split('T')[0]
startDay = date.split('-')[2] + '-' + date.split('-')[1] + '-' + date.split('-')[0]

date = new Date(endDay).toISOString().split('T')[0]
endDay = date.split('-')[2] + '-' + date.split('-')[1] + '-' + date.split('-')[0]

```

gives

```auto
"28-02-2005"

```

or probably better

```auto
let day = new Date("2005-03-01"); //YYYY-MM-DD
let end = new Date("2005-03-03");

while(day <= end){
    let startDay = day.setDate(day.getDate() - 1);
    let endDay = day.setDate(day.getDate() + 1);
    msg.payload= {
                    "startDay": timeStampToDate(startDay),
                    "endDay": timeStampToDate(endDay)
                }
    node.send(msg);

    day = new Date( day.setDate(day.getDate() + 1) )
}

function timeStampToDate(timeStamp) {
   let date = new Date(timeStamp).toISOString().split('T')[0]

   return date.split('-')[2] + '-' + date.split('-')[1] + '-' + date.split('-')[0]
}

```

---

<div class="post-metadata">

**Author:** ![dineshaj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dineshaj/32/53342_2.png) [@dineshaj](https://discourse.nodered.org/u/dineshaj)\
**Post date:** [9 March 2023 06:03 UTC](https://discourse.nodered.org/t/change-date-format/76212/6 "2023-03-09T06:03:23Z")

</div>

Wow. Perfect.

Thanks,

---

<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:** [23 March 2023 06:03 UTC](https://discourse.nodered.org/t/change-date-format/76212/7 "2023-03-23T06:03:28Z")

</div>

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