Sweet! This worked. Now I was wondering, what would you use to make a system where it updates each second? (While loop, or different, more efficient alternative?)
Also, how could I add a “Days” section? Sorry for so many questions, I’m pretty new to doing these types of things.
604800 seconds is a week. 86400 seconds is a day. Divide your time by 86400 and math.floor it. Add an extra placeholder in your format string of %.2i and add the calculation to the beginning.
You will also need to change the hour calculation to use modulus against 24.
local function toHMS(s)
return ("%02i:%02i:%02i:%02i"):format(s/86400, s/60^2%24, s/60%60, s%60)
end
local s = 604800
print(toHMS(s)) -- 07:00:00:00
local start = 8*3600 --time offset, when the first day started (8:00 AM)
local t0 = tick() - start
... --toHMS, setText
local update()
return tick() - t0
end
local resolution = 1 --update and show updates for 1 second
while true do
setText(toHMS(update()))
wait(resolution)
end
My solution was intented for a quick way to format, if you want to format for days and others, use os.date (wiki, lua.org).
Hey there! So I tested your method and, for some reason, this part:
was having issues. It said to remove the perentheses, then I saw that it looked like maybe it was meant to be a local function, so I put “local function update”, and it underlined
Hey there! So I tried using a that method (idk if you know how to fix this) and here it is:
local start = 604800 --time offset, when the first day started (8:00 AM)
local t0 = tick() - start
local function toHMS(s)
return ("%02i:%02i:%02i"):format( s/60^2, s/60%60, s%60)
end
local function setText(t)
script.Parent.TextLabel.Text = t
end
local function update()
return tick() - t0
end
local resolution = 1 --update and show updates for 1 second
while true do
setText(toHMS(update()))
wait(resolution)
end
Yeah, his code will count up from a specific time. If you want to count down until a specific time, you can take advantage of os.time’s optional parameter.
local dateTable = {month = 10, day = 5, year = 2019}
local t0 = os.time(dateTable)