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:
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.