Tim
September 4, 2014, 4:07pm
1
How can I display with a label component the value of a timer component in HH:MM:SS format?
I have found the following code as a starting point, but with this I get the minutes and hours rounded up.
[code]numberFormat(toInteger({Root Container.Timer.value}/3600),“00”)
“:”
numberFormat(toInteger(({Root Container.Timer.value}%3600)/60),“00”)
“:”
numberFormat({Root Container.Timer.value}%60,“00”)[/code]
Try using floor :
numberFormat(toInteger(floor({Root Container.Timer.value}/3600.0)),"00")
+ ":"
+ numberFormat(toInteger(floor(({Root Container.Timer.value}%3600)/60.0)),"00")
+ ":"
+ numberFormat({Root Container.Timer.value}%60,"00")
2 Likes
For future Googlers here is a very quick function to do this using divmod:
def ConvertSecondsToHoursMinutesSeconds(seconds):
"""
Copied from:
https://www.geeksforgeeks.org/python-program-to-convert-seconds-into-hours-minutes-and-seconds/
# Driver program
n = 65244
print(ConvertSecondsToHoursMinutesSeconds(n))
18:07:24
"""
min, sec = divmod(seconds, 60)
hour, min = divmod(min, 60)
return "%02d:%02d:%02d" % (hour, min, sec)
The original intent was to use an expression, not a script.
That said, new date functions have been added since 2014.
IMO, it can just as easily be done without script.
start from a date (midnight)
add minutes or seconds like you want (needs to be integers, if you want high precision, you can add milliseconds as integers)
format as “HH:mm:ss”
This doesn’t work if the hours value can reach more than 24 hours, but apart from that, it’s quite concise and easy to understand IMO.
dateFormat(
addSeconds(
getDate(1970,1,1),
toInt({Root Container.value} * 60)
),
"HH:mm:ss"
)
1 Like