Get play time os.date()

I trying to convert seconds to H:M:S, but I keep getting an 8, when it should be 0?

local function Convert(number)
	return os.date("%H:%M:%S", number)
end

and if I do say

print(Convert(108))

It should be
00:01:48, but I get 08:01:48

local osDate = os.date("!*t")
local hour, min, sec = osDate["hour"], osDate["min"], osDate["sec"]

Try this:

That returned 2 12 35 in the output :confused:

Well wt shud it print? Ignore ibnore

Try this:

local osDate = os.date("*t")
local hour, min, sec = osDate["hour"], osDate["min"], osDate["sec"]

Tge other one was utc time.

I had googled it, problem is that code is way too overdone. I’m looking for the shortest/cleanest way of doing this.

It should print how long I’ve been playing.

If I’ve been playing for 108 seconds, then it should print 00:01:48

Super compact

local s = 108		-- seconds
local N = ("%02i:%02i:%02i"):format(s/60^2, s/60%60, s%60)
print(N)			-- 00:01:48

Source: string.format

Autor

I wrote it from memory but had seen it elsewhere, so I looked and here is the original post, it’s the same but I compressed the part of string.format.

4 Likes

LOL i was fr doing it the hardest possible way, this is the shortest I could get it myself;

local format = function(integer)
	return string.format("%02i",integer)
end

local convert = function(seconds)
	local minutes = (seconds - seconds % 60) / 60
	seconds = seconds - minutes * 60
	local hours = (minutes - minutes % 60) / 60
	minutes = minutes - hours * 60
	return("%s:%s:%s"):format(format(hours),format(minutes),format(seconds))
end

print(convert(108)) --> 00:01:48

If you wrote that, where did you learn how to do that bc patterns hurt my brain too much

https://www.lua.org/pil/20.html

%0xi is zero padding for an integer where x is the number of zeros to pad. Zero gets added for every digit under the padding number.

1 Like