Converting secs to (h:min:sec)

Hey, I’m making a daily rewards system with a countdown label that loads up when a player joins the game. Preferably, the counter system should use the amount of seconds given to form the length of the countdown. The daily reward and countdown scripts themselves are done, but I’m struggeling to convert the amount of seconds left into a (h:min:sec) format. Any tips on how to complete the timer?

Here’s the part of the client sided script that forms the amount of seconds that I’d like to get converted:

local countdownLabel = script.Parent.Parent:WaitForChild("MainGUI").RewardTimeCount.TimeLabel
local timerEvent = game:GetService("ReplicatedStorage"):WaitForChild("Assets"):WaitForChild("Signals").TimerEvent

timerEvent.OnClientEvent:Connect(function(TimeSinceLastVisit,WaitTime)
	local TimeInSeconds
		if TimeSinceLastVisit > WaitTime then
			TimeInSeconds = WaitTime
			print("Reward countdown starting at "..TimeInSeconds.." seconds.")
		else
			TimeInSeconds = WaitTime - TimeSinceLastVisit
			print("Reward countdown starting at "..TimeInSeconds.." seconds.")
		end
	---?
	end
end)
36 Likes

Sounds fun. Here’s a bit of code I typed up that should work.

function Format(Int)
	return string.format("%02i", Int)
end

function convertToHMS(Seconds)
	local Minutes = (Seconds - Seconds%60)/60
	Seconds = Seconds - Minutes*60
	local Hours = (Minutes - Minutes%60)/60
	Minutes = Minutes - Hours*60
	return Format(Hours)..":"..Format(Minutes)..":"..Format(Seconds)
end

Tested examples for proof:
convertToHMS(1) → “00:00:01”
convertToHMS(59) → “00:00:59”
convertToHMS(60) → “00:01:00”
convertToHMS(61) → “00:01:01”
convertToHMS(3599) → “00:59:59”
convertToHMS(3600) → “01:00:00”
convertToHMS(3601) → “01:00:01”

213 Likes

Oh thanks @Uglypoe ! :slight_smile:

5 Likes

:golf:

local function toHMS(s)
	return string.format("%02i:%02i:%02i", s/60^2, s/60%60, s%60)
end
126 Likes

oh that works too, thanks @FieryEvent :wink:

does this do the same thing? just this little bit of code?

1 Like

How would I go about making a convertToMinutesSecondsMilliseconds(Seconds) function?

same thing but smaller numbers

2 Likes