How to string format?

Hello everyone,

I have a function where I need to create a simple 00:00:00 string. I received a time in decimal format (8.25, 8.50, etc...) and I want to convert to a String HH:MM:SS. I have everything, but I can't find how to have a leading 0. So when I have 8 hours or 0 min, I need to have 00 and not 0. So right now, it will print stuff like 9:30:00 and 9:0:00 instead of 09:00:00.

My current code is fairly simple:

var payload = {ontime: ""};
var msgTime = msg.payload.split(".");
var msgHour = msgTime[0];
var msgMin = msgTime[1]/100*60;
if (msgMin < 10){
msgMin = msgMin*10;
}
payload.ontime = msgHour+":"+msgMin+":00";
var newMsg = { payload:payload};
return newMsg;

Thanks!

try

msgHour.toString().padStart(2,"0")

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.