Date & Time Node Example

This is an example on a function node that extract separate date and time elements from a timestamp or a full date notation.
It can be used to learn or modified into what is needed.

[{"id":"e819ce763c9d3940","type":"inject","z":"d7ad1e7a2a4af03b","name":"test (US)","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":675,"y":450,"wires":[["435a3d7e0ad38370"]]},{"id":"9e14b4aa453ddc32","type":"debug","z":"d7ad1e7a2a4af03b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1040,"y":420,"wires":[]},{"id":"435a3d7e0ad38370","type":"function","z":"d7ad1e7a2a4af03b","name":"DateTime","func":"// msg.language is the language code in the form xx-YY. \n// Most of the language codes from here should work: http://www.lingoes.net/en/translator/langcode.htm\n// If no msg.language code is selected, then en-US is default.\n//\n// msg.payload contain the date\n// The script will try to parse a human written datetime, like december 1, 2033 11:25 AM GMT+1\n// If this can not be passed correctly, then it will try to read it as a timestamp value instead\n// Timestamps from within Node-Red works perfectly, but timestamps coming from HA might be missing some digits,\n// so you need to add the missing zeros in the end by *10000 or whatever is missing to reach the same number\n// of digits as Node-Red timestamps.\n\n// more info on the date objects used: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date\n// more info on localization of datetime: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat\n\n\nif (!msg.language) { msg.language = 'en-US'}\nvar input = Date.parse(msg.payload);\nif (isNaN(input)) { input = msg.payload }\nvar dateobject = new Date(input)\n\nmsg.timevalue = dateobject.valueOf();\n\nmsg.datestring = new Intl.DateTimeFormat(msg.language, { dateStyle: 'full', timeStyle: 'full' }).format(dateobject);\nmsg.date = dateobject.getDate(); //\n\nmsg.month = new Intl.DateTimeFormat(msg.language, { month: 'numeric' }).format(dateobject);\nmsg.monthshort = new Intl.DateTimeFormat(msg.language, { month: 'short' }).format(dateobject);\nmsg.monthlong = new Intl.DateTimeFormat(msg.language, { month: 'long' }).format(dateobject);\n\nmsg.year = new Intl.DateTimeFormat(msg.language, { year: 'numeric' }).format(dateobject);\nmsg.year2 = new Intl.DateTimeFormat(msg.language, { year: '2-digit' }).format(dateobject);\n\nmsg.day = dateobject.getDay();\nmsg.dayshort = new Intl.DateTimeFormat(msg.language, { weekday: 'short' }).format(dateobject);\nmsg.daylong = new Intl.DateTimeFormat(msg.language, { weekday: 'long' }).format(dateobject);\n\nmsg.hour24 = new Intl.DateTimeFormat(msg.language, { hour: 'numeric', hour12: false }).format(dateobject);\nmsg.hour24_2 = new Intl.DateTimeFormat(msg.language, { hour: '2-digit', hour12: false }).format(dateobject);\nmsg.hour12 = new Intl.DateTimeFormat(msg.language, { hour: 'numeric', hour12: true }).format(dateobject).split(\" \")[0];\nmsg.hour12_2 = new Intl.DateTimeFormat(msg.language, { hour: '2-digit', hour12: true }).format(dateobject).split(\" \")[0];\nmsg.hour12prefix = new Intl.DateTimeFormat(msg.language, { hour: '2-digit', hour12: true }).format(dateobject).split(\" \")[1];\n\nmsg.minute = dateobject.getMinutes();\nmsg.minute2 = new Intl.DateTimeFormat(msg.language, { minute: '2-digit' }).format(dateobject);\n\nmsg.second = dateobject.getSeconds();\nmsg.second2 = new Intl.DateTimeFormat(msg.language, { second: '2-digit' }).format(dateobject);\n\nmsg.timezone = new Intl.DateTimeFormat('en-US', { timeZoneName: 'short' }).format(dateobject).split(\" \")[1];\n\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":885,"y":420,"wires":[["9e14b4aa453ddc32"]]},{"id":"b5835288284fb793","type":"inject","z":"d7ad1e7a2a4af03b","name":"test (DK)","props":[{"p":"payload"},{"p":"language","v":"da","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":675,"y":405,"wires":[["435a3d7e0ad38370"]]},{"id":"0fe44da13360b12c","type":"inject","z":"d7ad1e7a2a4af03b","name":"test (GB)","props":[{"p":"payload"},{"p":"language","v":"en-GB","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":675,"y":495,"wires":[["435a3d7e0ad38370"]]},{"id":"f66e8186ba2ab711","type":"inject","z":"d7ad1e7a2a4af03b","name":"test (DK)","props":[{"p":"payload"},{"p":"language","v":"da-DK","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"24. januar 2024 13:22","payloadType":"str","x":675,"y":360,"wires":[["435a3d7e0ad38370"]]}]