How to stop countdown from going into negative minutes

I’m trying to make a global countdown wall, and it shows all 38 timezones. however, several timezones are off by 30 or 45 minutes rather than an hour. I’m using a number value to keep track of the minute offset, but eventually the numbers become negative numbers rather than rolling back to the next hour like I want them to. How do I fix this?

Here’s what the countdown shows, I want it to say 4 hours, 50 minutes instead of 5 hours, -10 minutes
Screenshot (212)

Here’s the Script:

local RunService = game:GetService("RunService")

local targetTime = os.time({year = 2020, month = 12, day = 24, hour = 12, min = 0, sec = 0})

local cityName = script.Parent.CityName.Value
local cDText = script.Parent.SurfaceGui.Countdown
local utcOffset = script.Parent.UtcOffset
local minuteOffset = script.Parent.MinutesOffset
local timeDif = 12

while true do
	wait(0.5)
	local diffSeconds = targetTime - os.time()
	local countdown = os.date('!*t', diffSeconds)
	local offset = countdown.hour - utcOffset.Value
	local hour = offset - timeDif
	local minutes = countdown.min - minuteOffset.Value
	
	if diffSeconds <= 0 then
		cDText.Text = ("It is already December 25th here")
	else
		cDText.Text = (("%i days, %i hours, %i minutes, and %i seconds until Dec. 25th"):format(countdown.yday, hour, minutes, countdown.sec))
	end
end

2 Likes

Why don’t you try utilizing the DateTime API instead?

It is much more precise and does avoid the conflicting issue of a leap year.


In the other hand, you should convert all values from seconds to the timer instead of setting offsets. Calculating how many seconds until the target date should solve that issue.

1 Like

Do you know how I would go about getting the amount of time left in the countdown using DateTime? I was using

local diffSeconds = targetTime - os.time()
local countdown = os.date('!*t, diffSeconds)

And then getting the numbers from countdown, but I’m not sure how I would go about converting that to DateTime, I may just be overlooking something on the API reference.

1 Like

I just realized that this is the issue. Why is there an offset for minutes? Also the offset for hours should not be done there as well.

The solution should be setting the offsets in seconds in the input, instead of calculating them post-output.

2 Likes