Simpletime Sub-flow to obtain and format date/time data

This lightweight sub-flow produces a Date/Time output in different formats for use in node-red flows.

The sub-flow creates a mytime node which can be inserted in any running flow, and adds a number of messages to the flow as described below.

Details

The additional payloads added to the flow, and their typical content format are;

  • msg.myrawdate: "2017-11-08T20:36:26.257Z"
  • msg.myepoch: 1510173386257
  • msg.mydate: "Wed Nov 08 2017"
  • msg.myhour: "20"
  • msg.myminute: "36"
  • msg.myminutes: "36:26"
  • msg.mysecond: "26"
  • msg.mytime: "20:36"
  • msg.mytimes: "20:36:26"

To introduce any of the messages into a flow, simply assign any, or any combination of the messages into a variable, such as; var seconds = msg.myseconds; or to use in a ui_text node add via mustache {{msg.mytimes}}

For more advanced timezone handling, node-red-contrib-moment is recommended.

[{"id":"28729743.b8d668","type":"subflow","name":"simpletime","info":"This lightweight sub-flow produces a Date/Time output in different formats for use in node-red flows.  \r\nThe sub-flow creates a `simpletime` node which can be inserted in any running flow, and adds the following messages to the flow; \r\n\r\n* msg.myrawdate: \"2017-11-08T20:36:26.257Z\"\r\n* msg.myepoch: 1510173386257\r\n* msg.mydate: \"Wed Nov 08 2017\"\r\n* msg.myhour: \"20\"\r\n* msg.myminute: \"36\"\r\n* msg.myminutes: \"36:26\"\r\n* msg.mysecond: \"26\"\r\n* msg.mytime: \"20:36\"\r\n* msg.mytimes: \"20:36:26\"\r\n\r\nAll other messages such as msg.payload & topic pass straight through the node unhindered.\r\n\r\nTo introduce any of the messages into a flow, simply assign any, or any combination of the messages into a variable(s), such as; `var seconds = msg.myseconds;`  \r\nor to use in a ui_text node add via mustache `{{msg.mytimes}}` \r\n\r\nFor advanced functionality, I would strongly recommend using the node-red-contrib-moment which is better equipped to handle edge-cases.\r\n\r\nAny feedback - please give me a shout - @rossoreed on [node-red slack](https://nodered.org/slack)","in":[{"x":174,"y":108,"wires":[{"id":"c5f4dfb.9ab032"}]}],"out":[{"x":488,"y":108,"wires":[{"id":"c5f4dfb.9ab032","port":0}]}]},{"id":"c5f4dfb.9ab032","type":"function","z":"28729743.b8d668","name":"simpletime","func":"var ts = msg.timestamp || Date.now();\nvar d = new Date(ts);\ndt = d.toDateString();\ne = d.getTime();\n\nvar a = d.toJSON().split(/\\D+/);\nvar h = a[3];\nvar m = a[4];\nvar s = a[5];\n\nvar tm = (h+\":\"+m);\nvar tms = (h+\":\"+m+\":\"+s);\nvar ms = (m+\":\"+s);\nmsg.myrawdate = d;\nmsg.myepoch = e;\nmsg.mysecond = s;\nmsg.myminute = m;\nmsg.myminutes = ms;\nmsg.myhour = h;\nmsg.mytime = tm;\nmsg.mytimes = tms;\nmsg.mydate = dt;\n\nreturn msg;","outputs":1,"noerr":0,"x":311,"y":108,"wires":[[]]},{"id":"f6cadf79.d24ef","type":"subflow:28729743.b8d668","z":"5a2d1188.691f1","x":407,"y":335,"wires":[["c021c5af.dee848"]]}]

Flow Info

Created 7 years ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • function (x1)
Other
  • subflow (x1)
  • subflow:28729743.b8d668 (x1)

Tags

  • node-red
  • moment
  • time
  • date
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option