For the past two days, I’ve been trying to make a countdown clock.
Let me explain how it is suppose to work: It should be something like 23 hours, 59 minutes, and 59 seconds remaining and so on and so on.
Edit: And when you open a GUI it will show the time until 24 hours has past.
How could I make something like this that last 24 hours/ a day?
I don’t think that’s correct. tick() is the function which isn’t universal - for making 24 hour cooldowns and daily rewards or anything like that, you will want to use os.time because it’s always in the UTC timezone.
I wasn’t really thinking this, I was think off a in-game countdown until a certain time, for example after a few minutes the timer would show: 23 hours, 36 minutes, 25 seconds remaining
You’d need a function that converts a number into hours, minutes, seconds.
function GetTime()
local hour = math.floor((tick()%86400)/60/60) local min = math.floor(((tick()%86400)/60/60-hour)*60)
if min < 10 then min = "0"..min end
return hour..":"..min
end
This only returns the time as a format hh:mm, but you can edit the ratios in this code and probably input a number in seconds and have it format it as hours, minutes, seconds.
You could use os.time() to achieve it, but it would be in seconds. to get it to minutes, just divide the number of seconds by 60, for hours, divide it by 60 x 60 (3600), for days, divide the number of seconds by 60 x 60 x 24 (86400). You could use math.floor to round the numbers, to not have 0.546 days or something like that.
local CurrentTime = os.time()
local NumberOfDays = math.floor(CurrentTime/(60*60*24)) -- ignore this if you don't need the number of days
local NumberOfHours = math.floor((CurrentTime - (NumberOfDays*24*60*60))/(60*60))
local NumberOfSeconds = math.floor((CurrentTime - ((NumberOfDays*24*60) + (NumberOfHours*24*60)/60)
local NumberOfSeconds = math.floor((CurrentTime - (NumberOfDays*24*60*60) + (NumberOfHours*24*60) + (NumberOfMins*60))
Note: We take away the time we already took away in days, so we don’t get “2 days 50 hours” when it should be “2 days 2 hours”
Edit: this is just an example, you could do “PreviousTime - CurrentTime” if you want to see how many days after or “CurrentTime - FinalTime” for how many days left
This would not work, os.time is how many seconds since January 1970, 00:00:00), UTC time, so it would only return the second, minutes, hours, and days since the epoch date.
local target = os.time() + 86400 --tomorrow at the same time (86400 = 60*60*24)
local target = os.time() + 86400
target = target - target % 86400 -- tomorrow at 00:00 in UTC time
On each second, or another preferred period:
Get the time difference:
local difference = target - os.time()
Format:
local function toHMS(s) --convert to hours, minutes, seconds and format to 0 left-padded integers separated by colons.
return ("%02i:%02i:%02i"):format(s/3600, s/60%60, s%60)
end
Format: (os.date() alternative)
local function toHMS(s) --convert to hours, minutes, seconds and format to 0 left-padded integers separated by colons.
s = os.date("!*t", s)
return ("%02i:%02i:%02i"):format(s.hour, s.min, s.sec)
end
Display: (print example)
print(toHMS(difference))
Example code
local target = os.time() + 86400
target = target - target % 86400 -- tomorrow at 00:00 in UTC time
local function toHMS(s) --convert to hours, minutes, seconds and format to 0 left-padded integers separated by colons.
return ("%02i:%02i:%02i"):format(s/3600, s/60%60, s%60)
end
while true do
print(toHMS(target - os.time()))
wait(1)
end
local target = os.time() + 86400 --tomorrow at the same time (86400 = 60*60*24)
local function toHMS(s) --convert to hours, minutes, seconds and format to 0 left-padded integers separated by colons.
return ("%02i:%02i:%02i"):format(s/3600, s/60%60, s%60)
end
while true do
print(toHMS(target - os.time()))
wait(1)
end