How to convert currenttimemillis to the date

NEWS

Here is an example of a node-red function that converts the current date into any format.

Code in the function:

! ````
// (1) converts timestamo to ISO date format
//msg.timestamp= new Date (). toISOString ()
! // (2) any date can be specified: here: YYYY-MM-DD HH: MM.SS
// (output format is compiled at the end)
var now = new Date ();
var year = now.getFullYear ();
var month = now.getMonth () + 1;
var day = now.getDate ();
var hour = now.getHours ();
var minute = now.getMinutes ();
var second = now.getSeconds ();
if (month.toString (). length == 1) {
var month = '0' + month;
}
if (day.toString (). length == 1) {
var day = '0' + day;
}
if (hour.toString (). length == 1) {
var hour = '0' + hour;
}
if (minute.toString (). length == 1) {
var minute = '0' + minute;
}
if (second.toString (). length == 1) {
var second = '0' + second;
}
! // Variant 1: if, for example, there is no existing payload
// should be overwritten, but rather a
// Date should be added.
! //msg.timestamp = year + '-' + month + '-' + day + '' + hour + ':' + minute + ':' + second;
! // Variant 2: Function outputs the current time as payload
msg.payload = year + '-' + month + '-' + day + '' + hour + ':' + minute + ':' + second;
! return msg;

In the example, the date of the message is not converted, but from the current time (Date ()).

Test and example for understanding:

Node-red flow:

! ````
[{"id": "76a3a5f9.895c5c", "type": "function", "name": "now current date", "func": "// (1) converts timestamo to ISO date format \ n // msg.timestamp = new Date (). toISOString () \ n \ n \ n // (2) any date can be specified: here: YYYY-MM-DD HH: MM.SS \ n // (output format is then compiled at the end ) \ nvar now = new Date (); \ nvar year = now.getFullYear (); \ nvar month = now.getMonth () + 1; \ nvar day = now.getDate (); \ nvar hour = now.getHours ( ); \ nvar minute = now.getMinutes (); \ nvar second = now.getSeconds (); \ nif (month.toString (). length == 1) {\ nvar month = '0' + month; \ n} \ nif (day.toString (). length == 1) {\ nvar day = '0' + day; \ n} \ nif (hour.toString (). length == 1) {\ nvar hour = '0' + hour; \ n} \ nif (minute.toString (). length == 1) {\ nvar minute = '0' + minute; \ n} \ nif (second.toString (). length == 1) {\ nvar second = '0' + second; \ n} \ n \ n \ n // Variant 1: if e.g. not an existing payload \ n // should be overwritten, but a \ n // date should be added. \ n \ n // msg.timestamp = year + '-' + month + '-' + day + '' + hour + ':' + minute + ':' + second; \ n \ n // Variant 2: Function outputs the current time as payload \ nmsg.payload = year + ' - '+ month +' - '+ day +' '+ hour +': '+ minute +': '+ second; \ n \ nreturn msg; "," outputs ": 1," x ": 1112.500015258789," y ": 136.66663551330566, "z": "9d9dc866.626238", "wires": [["9686a94e.697958", "7237ae9e.8dc85", "c557ecb1.3aa81"]]}, {"id": "9686a94e.697958", "type ":" ioBroker out "," name ":" "," topic ":" TestDatum "," ack ":" true "," autoCreate ":" true "," x ": 1311.6666450500488," y ": 135.00000190734863, "z": "9d9dc866.626238", "wires": []}, {"id": "7237ae9e.8dc85", "type": "debug", "name": "", "active": true, "console": "false", "complete": "false", "x": 1315.833396911621, "y": 98.33335304260254, "z": "9d9dc866.626238", "wires": []}, {"id" : "470bde4f.b8f42", "type": "inject", "name": "", "topic": "Example date format", "payload": "", "payloadType": "date", "repeat": "", "crontab": "", "once": false, "x": 638.7500076293945, "y": 138.33336067199707, "z": "9d9dc866.626238", "wires": [["2a14ac81.d5eb54", "c9fa1333.3605f"]]}, {"id": "2a14ac81.d5e b54 "," type ":" debug "," name ":" "," active ": true," console ":" false "," complete ":" false "," x ": 838.7500114440918," y ": 91.2500057220459, "z": "9d9dc866.626238", "wires": []}, {"id": "c557ecb1.3aa81", "type": "template", "name": "Example Object", "field ":" payload "," template ":" Topic: {{topic}} - \ nTimestamp from the inject node: \ n {{tst}} - \ nConversion from the function "now ...": \ n {{ payload}} "," x ": 1103.750015258789," y ": 228.75000381469727," z ":" 9d9dc866.626238 "," wires ": [[" 5bb61570.a449ec "]]}, {" id ":" c9fa1333. 3605f "," type ":" function "," name ":" Test msg.payload = msg.tst "," func ":" msg.tst = msg.payload; \ nreturn msg; "," outputs ": 1 , "x": 882.5000152587891, "y": 136.25000190734863, "z": "9d9dc866.626238", "wires": [["76a3a5f9.895c5c"]]}, {"id": "5bb61570.a449ec", " type ":" debug "," name ":" "," active ": true," console ":" false "," complete ":" false "," x ": 1332.5000190734863," y ": 227.50000381469727," z ":" 9d9dc866.626238 "," wires ": []}]

A request. Should I have misunderstood something, I am grateful for every hint and every correction!