Convert milliseconds to Hour:Minute:Second format

I want to display the accumulated value in a timer in hour:minute:second format. Timer is in milliseconds and is an INT4 OPC tag.

Here’s a script I use. I’m feeding it minutes but you could feed it milliseconds and divide by 1000.
It outputs a string in the form of HH:MM that use at the text in a label.

def GetHoursMinutesFromMinutes(MinIn) :
	# Return a string that represents HH:MM....
	try:
		Hours = MinIn // 60
		Minutes = MinIn % 60
		if Minutes < 10 :
			strMin = "0" + str(Minutes)
		else :
			strMin = str(Minutes)
		return str(Hours) + ":" + strMin
		
	except :
		return "Err"

One way to do it:

Make a label, and then tie the value to the following expression

stringFormat("%s:%s:%s",
	toInt(floor({TagPath/Timer Tag}/3600000)),
	toInt(floor(({TagPath/Timer Tag}%3600000)/60000)),
	toInt(floor(({TagPath/Timer Tag}%60000)/1000))
)

Not the most elegant, but it will get you there in expression language only

1 Like

Thank you, from this information I was able to get a display setup.

I ended up using three LED displays with the Number Format Pattern set to :00 to display colon and leading zeros and the value expressions set as follows:

Hour: floor({TagPath/Timer Tag}/3600000)
Minute: floor(({TagPath/Timer Tag}%3600000)/60000)
Second: floor(({TagPath/Timer Tag}%60000)/1000)