How to string.format() 00:00:00

Quick question, I’m not experienced in string.format() and wanna know how to format an integer value (like 5 for example) to 00:00:00 (in minutes, seconds, miliseconds).

Thanks

you can use the format %02i or %.2d which should format a number with an extra 0 (2 = 02, 20 = 20). another way to do this, is with os.date() and its format !%X

2 Likes
local seconds = 305 -- example value in seconds

local minutes = math.floor(seconds / 60) -- calculate the number of minutes
local remainingSeconds = seconds % 60 -- calculate the remaining seconds

local formattedTime = string.format("%02d:%02d:00", minutes, remainingSeconds)

print(formattedTime) -- output: 05:05:00

This should work fine.

2 Likes

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