How to make the in-game time the same as a real life timezone?

In order to set the game world time to be that of a timezone, you’ll have to use an external API to request the time for a given timezone, since roblox servers do not natively support that.

I would recommend making an HTTP Request to http://worldtimeapi.org/.
Before we begin, make sure that Allow HTTP Requests is on by going to Game settings > Security > Allow HTTP Requests.

looking at the world time API, we can request the CEST timezone using the following URL:
http://worldtimeapi.org/api/timezone/Europe/Amsterdam.

local httpService = game:GetService("HttpService")
local lighting = game:GetService("Lighting")

local URL = "http://worldtimeapi.org/api/timezone/Europe/Amsterdam"

local response

-- make sure nothing breaks if this returns an error.
pcall(function()
	response = httpService:GetAsync(URL)
end)

response will return a string with values for the timezone we called. A part of the string contains the unix time which we can use to get the time in that timezone.

-- grab "unixtime" from the string and turn it into a number.
function getUnixTime(str)
	local unixtime = tonumber(str:match('"unixtime":(%d+)'))
	return unixtime
end

then, we can turn the raw unixtime into a HH:MM:SS format, so we can use it with Lighting.TimeOfDay

function unixToTime(unixtime)
	return os.date("%H:%M:%S", unixtime)
end

local unixTime = getUnixTime(response)
local TimeOfDay = unixToTime(unixTime)

now we can plug TimeOfDay into Lighting:

lighting.TimeOfDay = TimeOfDay

and it should work, as long as worldtimeapi.org is up.

Full script:

local httpService = game:GetService("HttpService")
local lighting = game:GetService("Lighting")

local URL = "http://worldtimeapi.org/api/timezone/Europe/Amsterdam"

local response
local data

-- make sure nothing breaks if this returns an error.
pcall(function()
	response = httpService:GetAsync(URL)
end)

-- grab "unixtime" from the string and turn it into a number.
function getUnixTime(str)
	local unixtime = tonumber(str:match('"unixtime":(%d+)'))
	return unixtime
end

function unixToTime(unixtime)
	return os.date("%H:%M:%S", unixtime)
end

local unixTime = getUnixTime(response)
local TimeOfDay = unixToTime(unixTime)

print(TimeOfDay)
lighting.TimeOfDay = TimeOfDay
3 Likes