Global Countdown Until A Certain Time In A Certain Timezone (Roblox Together At Home Event)

Hi! I would like to create a countdown like the Roblox Together At Home event but, I’m only a beginner and i’m not so sure. I also don’t want to use os.time() since it’s not for a certain timezone.

If anybody could please help?

2 Likes

os.time() can be used to get a time from a certain timezone

os.time() will return the time in UTC (source: Os.time returns what system time? - #20 by Tiffblocks)

so that means if your timezone is UTC+4, you add 4 hours to what os.time() gives

example,

local utcPlus4 = os.time() + (4 * 60 * 60)

You can use https://www.epochconverter.com/ to grab the Unix timestamp for whatever time and timezone you want to compare to, then the difference is just timestamp - os.time().

Divide by 60 for minutes, then by 24 for days, etc.

1 Like

May you please give an example?

Sure! There is also the built-in function os.date which can be given a time argument to save you having to do the division by 60 and 24 yourself.

Let’s take the example of a countdown to 20 April 2020, at 12:00 (midday) BST (my local timezone right now).

Using the website I linked to, I enter the time in my local format, and select Local time for the timezone:
image

The timestamp 1587380400 is provided as the Epoch timestamp. This is always in UTC/GMT in seconds since 1 January 1970, so this is technically 11:00 on the 20 April 2020 in UTC due to the timezone difference with daylight savings.

In your code you would then find the current timestamp (again, in UTC) and calculate the difference:

local TARGET_TIME = 1587380400 -- 12:00 on 20 April 2020 BST

RunService.Heartbeat:Connect( function()
    local diffSeconds = TARGET_TIME - os.time()
    local countdown = os.date( '!*t', diffSeconds ) -- The first argument is important to say we're working in UTC so no offsets get applied

    -- here you would update a GUI or whatever it is you need to do
    print( ( 'There are %i days, %i hours, %i minutes and %i seconds until 12:00 BST on 20 April 2020!' ):format( countdown.yday, countdown.hour, countdown.min, countdown.sec ) )
end )

If your countdown will span years, you’d also need to account for the .year part of the response from os.date, though this method begins to produce inaccuracies if the year is a leap year. If you’re expecting it to be over a year I’d probably do the division manually, which would look like this:

local MIN = 60
local HOUR = MIN * 60
local DAY = HOUR * 24

function formatDiff( diffSeconds )
    local parts = {}
    parts.days = math.floor( diffSeconds / DAY ) -- Get days
    diffSeconds = diffSeconds - parts.days * DAY
    parts.hours = math.floor( diffSeconds / HOUR ) -- Get hours
    diffSeconds = diffSeconds - parts.hours * HOUR 
    parts.mins = math.floor( diffSeconds / MIN ) -- Get minutes
    diffSeconds = diffSeconds - parts.mins * MIN 
    parts.secs = diffSeconds -- Whatever's left is seconds
    return parts
end
-- note that days is now the total days, so two years would be around 730 days.

Sorry for the messiness - currently in the middle of a lot of university work. Let me know if any parts need clarifying.

10 Likes

hey, i made it like this

		local Days = math.floor(Time2/86400)
		local DayRest = Time2%86400
	
		local Hours = math.floor(DayRest/3600)
		local HourRest = DayRest%3600
		
		local Minutes = math.floor(HourRest/60)
		local MinuteRest = HourRest%60
		
		local Seconds = MinuteRest

just as another example of conversion

Just worth watching out for negatives when you go beyond the target time. with my example you can do a conditional statement on the seconds and if negative just show the absoluted values with “ago” instead of “to go”.

With modulus, you lose that ability as the value changes magnitude, for example:
-1240 => division and remainder => -20 mins and -40 seconds which can be shown as 20 mins and 40 secs ago.
-1240 => modulus => -20 mins and +20 seconds

How can I make it check if the countdown is completed?

It will be completed when the time remaining is less than or equal to zero:

if diffSeconds <= 0 then
    -- show completed stuff
else
    -- show the countdown
end

Hello! I’ve come across this system and find it very intuitive, but I’ve run into an issue.

When I run the code I keep getting this error:

If anyone could help I’d appreciate it!

Can you include the code you ran?

1 Like

Thanks for getting back to me so quickly!
Turns out I didn’t save the baseplate I was testing the code in and I ran it today and It worked just fine. I probably made a mistake when using the code, but it works, and thanks again!

Quick question though

Would you know how I could make the timer restart every 6 hours? Like if some sort of multiplication or addition would make the turrent time stamp 6 hours ahead or 6 hours from the end time.