Integer to time (HH:MM:SS)

I have an integer that I want to convert into time(HH:MM:SS)

It's a countdown timer (in seconds) from 24 hours == 86400 seconds to zero, and I've tried to do this in a function block, but don't get it correct! Grateful for help in this case.

Kind regards
Hakan

So what have you done so far?Please post the code from your function so we can see where you have got to.

Hi!

I have got the code to work, but I would like to get padding (.padStart (2, "0"):wink: to work to avoid 10: 3: 37 instead of 10:03:37. Someone who has any idea how to implement it?

// Time HH:MM:SS from number

// Hours
var Hours = ((msg.payload)/3600);
Hours = Math.floor(Hours);

// Minutes
var Minutes = (msg.payload-(Hours*3600))/60;
Minutes = Math.floor(Minutes);

// Seconds
Seconds = (msg.payload-(Hours3600)-(Minutes60));

// Concatenate
msg.payload = Hours + ":" + Minutes + ":" + Seconds;

return msg;

For javascript problems a quick search of stackoverflow usually provides the answer

Thanks a lot! It worked perfect... :slight_smile:

// Time HH:MM:SS from number

// Hours
var Hours = ((msg.payload)/3600);
Hours = Math.floor(Hours);

// Minutes
var Minutes = (msg.payload-(Hours*3600))/60;
Minutes = Math.floor(Minutes);

// Seconds
Seconds = (msg.payload-(Hours3600)-(Minutes60));

// Concatenate
var Time = ('0' +Hours).slice(-2)+':'+('0' +Minutes).slice(-2)+':'+('0' +Seconds).slice(-2);

msg.payload = Time;
return msg;

My solution is:

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

return msg;

which strips out everything else in the incoming message and doesn’t answer the question that was posed…