Goodmorning. I’m needing a little help on writing some bacic script to format a timer value into a clock format. For example, I have a “Raw_Runtime” tag that displays the machine run time in milliseconds based off the running bit. I would like to convert that value to display as ##:##:## (HH:MM:SS).
You can use the INT command in Basic to round a non-integer value down to just the integer component. So a command like
Seconds% = INT(Raw_Runtime@ / 1000) will yield the number of seconds that the machine has been running.
Once you have integer values for hours, minutes, and seconds, you can pad the numbers with 0s using SFMT. If Minutes% = 7, you can use
SFMT Minutes%, 30, 0, "%02d" to get the string 07. You can disregard the middle two parameters, they just indicate that you’re converting an integer to a formatted string and that the output size should be whatever the size of the formatted string is.
Finally, with formatted strings for the hours, minutes, and seconds, you can combine them all and save them to a tag with
Runtime@ = HourStr$ + ":" + MinStr$ + ":" + SecStr$
You can then use ONCHANGE to trigger code whenever Raw_Runtime’s value changes so that you can convert its value and update a string tag to display the formatted time.
For more information on Basic, check out the reference guide here: RG-0006-01-EN - Programming Reference Guide
Thank you very much. I’ll give this a shot.
The only issue I may see is that it would always show full running seconds and minutes. I would like to format it to look like a clock run time. So, each time the seconds or minutes value reached >59 it would increment the following number and start over at 0.
You’ll have to fiddle with the math a bit, but this should be doable with a process like:
- Convert to total number of seconds
- Extract number of hours, then subtract (hours * 3600) from the total seconds
- Extract number of minutes, then subtract (minutes * 60) from the total seconds
- The remaining value of total seconds should be < 60
Thank you. I’ll see if I can make this work. Much appreciated!
Happy to help, let us know how it goes.
One other thing came to mind. Another approach that might work well is using the MOD command, which gives you a remainder.
4300 MOD 3600 yields 700, for example, so you could use that to separate out the minutes and seconds remaining after using division to get the hours and minutes.