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


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)"



take a look at node-red-contrib-moment



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".



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:


will display in the dashboard:


[{"id":"ef0542b2.1996c","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"1d83ae9a.88fb91","type":"comment","z":"ef0542b2.1996c","name":"","info":"Source [ANGULAR JS](\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"}]
1 Like