Skip to content

Instantly share code, notes, and snippets.

@mathcoll
Created August 17, 2014 18:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathcoll/ba071f83d91d2b065074 to your computer and use it in GitHub Desktop.
Save mathcoll/ba071f83d91d2b065074 to your computer and use it in GitHub Desktop.
Convert timestamp to array hours, minutes, day, month, year

Input a timestamp (default is the current timestamp) in milliseconds. The node will output an object of Hours, Minutes, mSeconds, Day, Month and Year.

[{"id":"4b27b7b.36083c8","type":"inject","name":"Force","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":226,"y":684,"z":"995dcaed.11032","wires":[["f7b2e05e.f958f8"]]},{"id":"2f5ae7e2.12b908","type":"debug","name":"output","active":true,"console":"false","complete":"true","x":818,"y":612,"z":"995dcaed.11032","wires":[]},{"id":"47a4278c.65ce68","type":"function","name":"timeConvert","func":"if ( !msg.timestamp ) msg.timestamp = Math.round(+new Date());\n\nvar dt = new Date(msg.timestamp);\nvar msg = {\n\t'month':\tdt.getMonth() + 1,\n\t'day':\t\tdt.getDate(),\n\t'year':\t\tdt.getFullYear(),\n\t'hours':\tdt.getHours(),\n\t'mins':\t\tdt.getMinutes(),\n\t'msecs':\tdt.getMilliseconds()\n}\n\nreturn msg;","outputs":1,"x":622,"y":612,"z":"995dcaed.11032","wires":[["2f5ae7e2.12b908"]]},{"id":"f7b2e05e.f958f8","type":"function","name":"set timestamp","func":"msg.timestamp=1376763133 * 1000;\nreturn msg;","outputs":1,"x":410,"y":684,"z":"995dcaed.11032","wires":[["47a4278c.65ce68"]]},{"id":"3bde431b.451a3c","type":"inject","name":"Force","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":226,"y":612,"z":"995dcaed.11032","wires":[["47a4278c.65ce68"]]}]
@YamisPL
Copy link

YamisPL commented Aug 9, 2021

This is good. I changed the repetition every second and the write to the global variable.

if ( !msg.timestamp ) msg.timestamp = Math.round(+new Date());

var dt = new Date(msg.timestamp);
global.set("Time", {
'month': dt.getMonth() + 1,
'day': dt.getDate(),
'year': dt.getFullYear(),
'hours': dt.getHours(),
'mins': dt.getMinutes(),
'msecs': dt.getMilliseconds()
})

return global.Time;

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