Converting secs to (h:min:sec)

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”

226 Likes