How can I make a 24 Hour Countdown

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?

5 Likes

you could use os.Time() to see how many hours until a certain day.

2 Likes

doyouevenbruh33 Idea is ok, but it has a major flaw. The os.time isn’t universal. I would recommend using tick().

An example script:

--time since server started

local start = tick()

while true do
print(tick()-start)
wait(1)
end

Useful Links:

1 Like

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.

5 Likes

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.

2 Likes

Is it possible you could also return the amount of 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

4 Likes

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.

Edit: I just saw your edit, let me try it.

Make sure there is a minute and a second, not two seconds

My output is:
1 hours, 6477 minutes, 396513 seconds.

How can I fix this?

I don’t know how to but I believe you may have to round the seconds to the nearest ones? (ex. Seconds = 129343434, round it to 12?

  1. Calculate the target time once
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
  1. 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
3 Likes

Works perfectly, just a few bugs.

How can I make target in 24 hours, not tomorrow at 00:00 in UTC time

Use this version:

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
11 Likes

It’s working now, thanks. <3 Have a great day and be safe.