How can I convert seconds into an time format?

Hello, I am creating a tempbanning system and I am trying to show how long is left with a format of the date and time it ends, the time is in seconds, how can I do this? I been messing around and searched around and can’t figure anything out.

Something like this? Converting secs to (h:min:sec)

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

To change seconds to minutes, divide by 60, to change minutes to seconds, multiply by 60.

Can I format it to show like this?

1d 2h 3m or something like that?

Yes, Probably by doing something like s/(60^2)/24 for the hour value.

(Not tested but a guess)

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

I think youd also have to account for the value the days take up like this:

return string.format("%02i:%02i:%02i", s/60^2/24, s/60^2%24, s/60%60)
1 Like

That doesn’t seem to work, I tried that and all values are 0

It seems to work for me
Are you sure that its above 60 so thered be a minute to display? Or do you want it to display day:hour:minute:second instead of day:hour:minute

Day hour minute second, but I am playing to if the person only has 1 minute it only shows the minute and seconds, and if only a hour then doesn’t show the day.

I have this but it returns negative, even when I use absolute value

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).."h"..Format(Minutes).."m"..Format(Seconds).."s"
end

i dont understand whats the red percentages for

1 Like