How To Make A Specific Timezone Clock

How To Make A Specific Timezone Clock


Hello there!

In this post, you’ll be learning how to make a simple clock that is based on a specific timezone regardless of a client’s timezone.

For this tutorial, we will be trying to get the timezone of Philippines (but you can use any other location that you would like).

Coding The Clock


To make this clock, we can use os.date(string: formatString, int time) which returns a dictionary that has all the information we are going to need based on a specified time.

Normally, it returns time based on the current local timezone. But we can tell it to give us UTC time ("!*t") instead of local time ("*t").

local date = os.date("!*t", os.time())

“But wait, doesn’t this only return UTC time?”

Yes it does! Which is why we need to add an offset based on a timezone you want. To do this, you’re going to want to search up the name of the country, state, and etc. and find out it’s UTC time.

Something like this should pop up.

As you can see for the example, Philippines is 8 hours ahead of UTC. And each hour has 3600 seconds. And since os.time() returns the amount of seconds since the Unix epoch under UTC time, we just multiply 3600 by 8 and add the offset to it!

local date = os.date("!*t", os.time() + (3600 * 8))

Nice! Now all that’s left to do is to use this and make it readable.

Since the returned hour is military time, we are going to use a simple formula to convert it to standard time. If the given hour is bigger than 12, subtract 12 from that number and voila, you have your standard hour. Then from that, we can just input whether we use AM or PM.

hour - An integer between 1 and 24 describing the current hour of the day.

We are also going to have to format the returned minutes because it returns the exact integer of the minute (i.e. if we just went with just adding the minute and the integer was a single digit, the result would be 10:5 AM instead of 10:05 AM) So what we do is we check how much digits is the returned minutes. If it is less than 2, add 0 in front of the minute, otherwise, do no changes.

min - An integer between 0 and 59 describing the current minute of the hour.
local function MilitaryToStandard(hour)
	return if hour > 12 then hour - 12 else hour, if hour > 12 then true else false
end

local function ConvertTo2Digit(digit)
	return if #tostring(digit) < 2 then 0 .. digit else digit
end

local date = os.date("!*t", os.time() + (3600 * 8))
local min = ConvertTo2Digit(date.min)
local hour, isPM = MilitaryToStandard(date.hour)

Then last but not the least, just concatenate all of this information into one string. Your code should look like this now!

local function MilitaryToStandard(hour)
	return if hour > 12 then hour - 12 else hour, if hour > 12 then true else false
end

local function ConvertTo2Digit(digit)
	return if #tostring(digit) < 2 then 0 .. digit else digit
end

local date = os.date("!*t", os.time() + (3600 * 8))
local min = ConvertTo2Digit(date.min)
local hour, isPM = MilitaryToStandard(date.hour)
local result = hour .. ":" .. min .. if isPM then " PM" else " AM"

Conclusion


And there you have it! Congratulations, you just made your own clock!

If you are facing any problems, please ask me about it!

12 Likes

Im confused, since I am a beginner scripter, I dont see where you put the script

As in, a clock model? A textlabel?

So can you put a final product video pls, pictures work too

3 Likes

It’s basically just a a system which allows you to get a timezone time (and you can then implement it into a clock however u want to).

2 Likes

The variable “result” returns a string formatted like this: “3:25 PM”.

You can run this code in any type of script.

1 Like

cant we like change the offset depending on the players timezone? like you said that about philipines but without changing it manually cant we do it automatically?

1 Like

Despite using a while true do loop, the clock doesn’t seem to want to update.


I’ll just put the “while true do” at the top of the script rather than at the bottom, I guess.

1 Like

Yes you can do that, but what if you want a specific timezone for all players (maybe a horror game located in a specific state)? Which is why I made this tutorial.

Could you show your clock code?

Updated the tutorial, I forgot that os.date().min can return single digit numbers.

It’s alright, man. I already got it solved.

1 Like

Ok then can you please tell me how can I do that? I am not good with time.

1 Like

Do you want a specific timezone or a local one?

local one yes I forgor the word :skull:

1 Like

I am pretty sure you can just do

local date = os.date("*t", os.time())

ok thanks.

1 Like

This is fine server-side but not client side as "*t" means local-time; this system is to specify a specific timezone so if you have the possibility of running it in a different env (client side) and getting an incorrect result you should instead use "!*t" for resiliance and clarity.

I did like this, and it works:
This will display your current time in your timezone.

(why don’t they have os.hour() or something easier)

local dt = DateTime.now()

local sec = dt:FormatLocalTime("ss", "en-us")
local min = dt:FormatLocalTime("mm", "en-us")
local hour = dt:FormatLocalTime("HH", "en-us")

local function ConvertTo2Digit(digit)
	return if #tostring(digit) < 2 then 0 .. digit else digit
end

while true do
	sec = sec + 1
	if sec == 60 then
		min = min + 1
		sec = 00
	end
	if min == 60 then
		hour = hour + 1
		min = 00
	end
	if hour == 24 then
		hour = 00
	end
	sec = ConvertTo2Digit(sec)
	min = ConvertTo2Digit(min)
	hour = ConvertTo2Digit(hour)
	print(hour..":"..min..":"..sec)
	wait(1)
end

Works, but on mobile when i changed my time, it shows that time instead of the timezone i put