# Using Function node to format date in YYYY-MM-DD

**URL:** <https://discourse.nodered.org/t/using-function-node-to-format-date-in-yyyy-mm-dd/1587>\
**Category:** General\
**Created:** [15 July 2018 16:26 UTC](https://discourse.nodered.org/t/using-function-node-to-format-date-in-yyyy-mm-dd/1587 "2018-07-15T16:26:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bidyutper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bidyutper/32/1183_2.png) [@bidyutper](https://discourse.nodered.org/u/bidyutper)\
**Post date:** [15 July 2018 16:26 UTC](https://discourse.nodered.org/t/using-function-node-to-format-date-in-yyyy-mm-dd/1587/1 "2018-07-15T16:26:51Z")

</div>

Hi, how to format In YYYY-MM-DD using function node  
Found a Js function for the same:

function convert(str) {  
var mnths = {  
Jan:"01", Feb:"02", Mar:"03", Apr:"04", May:"05", Jun:"06",  
Jul:"07", Aug:"08", Sep:"09", Oct:"10", Nov:"11", Dec:"12"  
},  
date = str.split(" ");  
return [date[3], mnths[date[1]], date[2] ].join("-");  
}

convert("Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)");  
//out put-\> "2011-06-09"

My incomming date format is

"Sun Jul 15 2018 21:23:14 GMT+0530 (IST)"

---

<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:** [15 July 2018 18:00 UTC](https://discourse.nodered.org/t/using-function-node-to-format-date-in-yyyy-mm-dd/1587/2 "2018-07-15T18:00:06Z")

</div>

take a look at [node-red-contrib-moment](https://flows.nodered.org/node/node-red-contrib-moment)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [15 July 2018 19:46 UTC](https://discourse.nodered.org/t/using-function-node-to-format-date-in-yyyy-mm-dd/1587/3 "2018-07-15T19:46:52Z")

</div>

As mentioned, you can use my node-red-contrib-moment node to do this easily.

Alternatively, look up the `toISOString` JavaScript function and then split the resulting string on the letter "T".

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [15 July 2018 21:57 UTC](https://discourse.nodered.org/t/using-function-node-to-format-date-in-yyyy-mm-dd/1587/4 "2018-07-15T21:57:55Z")

</div>

If you want just to display the formatted date in the dashboard then it is straightforward using the formatting options from the ui text node:

![r-02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/8fc53254cac0cc8befde256378d2e60f0b04dcb8.PNG)

will display in the dashboard:

![r-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3bdc88a6f36cd384d994fa44032a2440a2e4458d.PNG)

```auto
[{"id":"ef0542b2.1996c","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"1d83ae9a.88fb91","type":"comment","z":"ef0542b2.1996c","name":"","info":"Source [ANGULAR JS](https://docs.angularjs.org/api/ng/filter/date).\n\nFormats date to a string based on the requested format.\n\nformat string can be composed of the following elements:\n\n* 'yyyy': 4 digit representation of year (e.g. AD 1 => 0001, AD 2010 => 2010)\n* 'yy': 2 digit representation of year, padded (00-99). (e.g. AD 2001 => 01, AD 2010 => 10)\n* 'y': 1 digit representation of year, e.g. (AD 1 => 1, AD 199 => 199)\n* 'MMMM': Month in year (January-December)\n* 'MMM': Month in year (Jan-Dec)\n* 'MM': Month in year, padded (01-12)\n* 'M': Month in year (1-12)\n* 'LLLL': Stand-alone month in year (January-December)\n* 'dd': Day in month, padded (01-31)\n* 'd': Day in month (1-31)\n* 'EEEE': Day in Week,(Sunday-Saturday)\n* 'EEE': Day in Week, (Sun-Sat)\n* 'HH': Hour in day, padded (00-23)\n* 'H': Hour in day (0-23)\n* 'hh': Hour in AM/PM, padded (01-12)\n* 'h': Hour in AM/PM, (1-12)\n* 'mm': Minute in hour, padded (00-59)\n* 'm': Minute in hour (0-59)\n* 'ss': Second in minute, padded (00-59)\n* 's': Second in minute (0-59)\n* 'sss': Millisecond in second, padded (000-999)\n* 'a': AM/PM marker\n* 'Z': 4 digit (+sign) representation of the timezone offset (-1200-+1200)\n* 'ww': Week of year, padded (00-53). Week 01 is the week with the first Thursday of the year\n* 'w': Week of year (0-53). Week 1 is the week with the first Thursday of the year\n* 'G', 'GG', 'GGG': The abbreviated form of the era string (e.g. 'AD')\n* 'GGGG': The long form of the era string (e.g. 'Anno Domini')\n\nformat string can also be one of the following predefined localizable formats:\n\n* 'medium': equivalent to 'MMM d, y h:mm:ss a' for en_US locale (e.g. Sep 3, 2010 12:05:08 PM)\n\n* 'short': equivalent to 'M/d/yy h:mm a' for en_US locale (e.g. 9/3/10 12:05 PM)\n* 'fullDate': equivalent to 'EEEE, MMMM d, y' for en_US locale (e.g. Friday, September 3, 2010)\n* 'longDate': equivalent to 'MMMM d, y' for en_US locale (e.g. September 3, 2010)\n* 'mediumDate': equivalent to 'MMM d, y' for en_US locale (e.g. Sep 3, 2010)\n* 'shortDate': equivalent to 'M/d/yy' for en_US locale (e.g. 9/3/10)\n* 'mediumTime': equivalent to 'h:mm:ss a' for en_US locale (e.g. 12:05:08 PM)\n* 'shortTime': equivalent to 'h:mm a' for en_US locale (e.g. 12:05 PM)\n\nformat string can contain literal values. These need to be escaped by surrounding with single quotes (e.g. \"h 'in the morning'\"). In order to output a single quote, escape it - i.e., two single quotes in a sequence (e.g. \"h 'o''clock'\").\n\nAny other characters in the format string will be output as-is.","x":270,"y":100,"wires":[]},{"id":"37becd29.ddc912","type":"ui_text","z":"ef0542b2.1996c","group":"47c75140.4ddf1","order":0,"width":"5","height":"1","name":"","label":"","format":"{{payload | date:\"yyyy-MM-dd\"}}","layout":"row-left","x":470,"y":160,"wires":[]},{"id":"1d800e20.4afbf2","type":"inject","z":"ef0542b2.1996c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"","x":290,"y":160,"wires":[["37becd29.ddc912"]]},{"id":"47c75140.4ddf1","type":"ui_group","z":"","name":"Formating Date","tab":"1a66657b.d6262b","disp":true,"width":"5","collapse":false},{"id":"1a66657b.d6262b","type":"ui_tab","z":"","name":"Formating Date","icon":"dashboard"}]

```

---

<div class="post-metadata">

**Author:** ![dirk.schmeckthal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dirk.schmeckthal/32/10735_2.png) [@dirk.schmeckthal](https://discourse.nodered.org/u/dirk.schmeckthal)\
**Post date:** [25 September 2019 19:41 UTC](https://discourse.nodered.org/t/using-function-node-to-format-date-in-yyyy-mm-dd/1587/5 "2019-09-25T19:41:38Z")

</div>

I have this working, but where do i switch from en\_US locale to german?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [9 July 2020 12:40 UTC](https://discourse.nodered.org/t/using-function-node-to-format-date-in-yyyy-mm-dd/1587/7 "2020-07-09T12:40:55Z")

</div>


