Convert seconds to hh:mm:ss


#1

Here is a bit of JavaScript in a function Node that will convert seconds (less than 1 days worth, ie 86400) into hh:mm:ss.
The upstream Node returns the seconds you want, the Nodes following this must be expecting a string.

In my case, the upstream Node was the result of an API call.
The downstream Node was a groov Data Store.

Here is what you are going to import;

I have put an inject node with a hard coded example of 2345 seconds.
You can see in the debug tab to the far right the output of 39 minutes and 5 seconds.

Here is the flow;

[{“id”:“e322e22c.55f208”,“type”:“function”,“z”:“c9ab873d.d3351”,“name”:“Convert seconds to hh:mm:ss”,“func”:"// Message from preceeding node is in seconds - should be less than 24 hours (86400 seconds).\nvar time = (new Date(msg.payload * 1000)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0];\n// Convert the ‘number’ (it now has two : in it) to a string by adding to an empty string.\nmsg.payload = time + ‘’;\n// Pass on the string to the next node, what ever that may be. (groov Data Store for example).\nreturn msg;\n\n",“outputs”:1,“noerr”:0,“x”:382,“y”:292,“wires”:[[“4e5060d3.7f8d”]]},{“id”:“4e5060d3.7f8d”,“type”:“debug”,“z”:“c9ab873d.d3351”,“name”:"",“active”:true,“console”:“false”,“complete”:“false”,“x”:637,“y”:292,“wires”:[]},{“id”:“8347e06b.333038”,“type”:“inject”,“z”:“c9ab873d.d3351”,“name”:“seconds”,“topic”:"",“payload”:“2345”,“payloadType”:“num”,“repeat”:"",“crontab”:"",“once”:false,“x”:150,“y”:292,“wires”:[[“e322e22c.55f208”]]}]


#2

hi i need convert count number in hours and minuts …can you help me?
i look yours project but dont’go…why?
thanks


#3

It looks like something went wrong with the json text, here is a fixed version:

[{"id":"49fd170f.07e188","type":"inject","z":"7e28ca31.3eaf94","name":"","topic":"","payload":"86399","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":200,"wires":[["e822b41b.999828"]]},{"id":"e822b41b.999828","type":"function","z":"7e28ca31.3eaf94","name":"[s] to [hh:mm:ss]","func":"// Message from preceeding node is in seconds - should be less than 24 hours (86400 seconds).\nvar time = (new Date(msg.payload * 1000)).toUTCString().match(/(\\d\\d:\\d\\d:\\d\\d)/)[0];\n// Pass on the string to the next node, what ever that may be. (groov Data Store for example).\nmsg.payload = time;\nreturn msg;","outputs":1,"noerr":0,"x":420,"y":200,"wires":[["ea54785e.aeb5c8"]]},{"id":"ea54785e.aeb5c8","type":"debug","z":"7e28ca31.3eaf94","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":610,"y":200,"wires":[]}]