Skip to content

Instantly share code, notes, and snippets.

@MDAR
Last active December 31, 2025 11:48
Show Gist options
  • Select an option

  • Save MDAR/0cb5ffccda571e0e77de81b12866efa6 to your computer and use it in GitHub Desktop.

Select an option

Save MDAR/0cb5ffccda571e0e77de81b12866efa6 to your computer and use it in GitHub Desktop.
Function node to add Time and Date to payload

Having looked at the forums for solutions, based on these posts I created this Function node to add a Date & Time to a payload.

I thought that by adding it here, others can adapt it to suit their own needed.

You can set the timezone Use or ignore the part in the msg.date

This sample contains

An inject node to trigger, with msg.payload and msg.topic

A function node creates a DateTime value and adds it via msg.date

A function node compiles

  • msg.topic
  • msg.payload
  • msg.date into a msg.payload

Debug node to show the resulting msg.payload

#####Day names are in English, but easy to adapt to any language in the Switch section

[
{
"id": "b66891715f7af89a",
"type": "function",
"z": "78d2ac1488bd39a5",
"name": "Add TIME and DATE",
"func": "// Get the current time and convert it to text\nvar now = new Date();\n\nlet localDate = new Date(now.toLocaleString('en-EN', { timeZone: \"Europe/London\" }));\nlet offset = now.getTime() - localDate.getTime();\nnow.setTime( now.getTime() + offset );\n\nvar yyyy = now.getFullYear();\nvar mm = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based\nvar dd = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nvar hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nvar mmm = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nvar ss = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\n\nvar dayNo = now.getDay();\nvar day = \"\";\n\n\nswitch (dayNo) {\ncase 1:\nday =\"Mon\";\n break;\ncase 2:\nday =\"Tue\";\n break;\ncase 3:\nday =\"Wed\";\n break;\ncase 4:\nday =\"Thur\";\n break;\ncase 5:\nday =\"Fri\";\n break;\ncase 6:\nday =\"Sat\";\n break;\ncase 7:\nday =\"Sun\";\n break;\n\n\n default:\nday =\"Unknown\";\n break;\n}\n\n\n\nnode.status({fill:\"grey\",shape:\"dot\",text:yyyy+\".\"+mm+\".\"+dd+\"(\"+ day+\") \"+hh+\":\"+mmm+\":\"+ss});\nmsg.date = yyyy+\".\"+mm+\".\"+dd+\"(\"+ day+\") \"+hh+\":\"+mmm+\":\"+ss;\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 580,
"y": 740,
"wires": [
[
"698e8acfede4bc72",
"cc0201d807c141c9"
]
]
},
{
"id": "090b2c636439bac8",
"type": "inject",
"z": "78d2ac1488bd39a5",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "Test Trigger",
"payload": "true",
"payloadType": "bool",
"x": 320,
"y": 740,
"wires": [
[
"b66891715f7af89a"
]
]
},
{
"id": "698e8acfede4bc72",
"type": "debug",
"z": "78d2ac1488bd39a5",
"name": "Date",
"active": true,
"tosidebar": false,
"console": false,
"tostatus": true,
"complete": "true",
"targetType": "full",
"statusVal": "date",
"statusType": "msg",
"x": 830,
"y": 660,
"wires": []
},
{
"id": "cc0201d807c141c9",
"type": "function",
"z": "78d2ac1488bd39a5",
"name": "Compile payload",
"func": "msg.payload = msg.payload +\" from \"+msg.topic+\" @ \"+msg.date;\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 850,
"y": 740,
"wires": [
[
"589bad3243edf638"
]
]
},
{
"id": "589bad3243edf638",
"type": "debug",
"z": "78d2ac1488bd39a5",
"name": "Compiled",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "true",
"targetType": "full",
"statusVal": "date",
"statusType": "msg",
"x": 1080,
"y": 740,
"wires": []
}
]
@MDAR
Copy link
Author

MDAR commented Dec 31, 2025

There was a small typo in the DateTime function Switch for adding the day name.

That has been corrected in this Gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment