How do I format a timer?

I’m making a timer with minutes in it, however it would look and count like this normally:

NOTE: The example timers in this post are 5 minutes (300 seconds), just incase you need to know that.

300, 299, 298, you name it.

I want to format it to make it look and count like this:

5:00, 4:59, 4:58, you name it.

Anyone know? It probably involves something with string.sub but I don’t know exactly what it is.
If you know, please let me know in a reply to this post.

Well you could try something like this:

local totalSeconds = 300

while totalSeconds > 0 do
     local minutes = math.floor(totalSeconds/60)
     local seconds = totalSeconds - minutes*60
     local timeString = minutes .. ":" .. seconds
     totalSeconds -= 1
     wait(1)
end

Haven’t tested it much, but that should work.

1 Like
local function formatTime(seconds)
	local min = math.floor(seconds / 60)
	local sec = seconds % 60
	return string.format("%i:%02i", min, sec)
end
print(formatTime(283)) -- 4:43
5 Likes
local function formatTime(s)
	return string.format("%.f:%02d", s/60, s%60)
end

print(formatTime(300)) --5:00
2 Likes