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

I have an Airline game in Roblox and i would like to have the TimeOfDay the same as the CEST timezone. I have seen this in other games but im not sure how to implement it.

I have found videos and other topics about this, but it was only to get the local time of the player to display on a textlabel.

1 Like

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
2 Likes

This works very well, one question though. How would i get the weather?

Hi, sorry for the late answer.

Getting the weather is the same principle, find an API (preferably free) to fetch weather information from.
I did some digging and found an API called weatherapi.com

Here’s the documentation for their api:

from there, we can gather that you need to make an account and then grab your API key, we will need this to actually be able to make requests. Be careful though, sites like these usually don’t like it when you send a lot of requests in a short time frame.

you can grab your API key by creating an account, then going to your account settings and checking your API key there.

looking at the documentation, we can gather that we need this link to access the current weather data:

http://api.weatherapi.com/v1/current.json

we would then need to give this API endpoint the correct request headers. In this case, we would use a script like this to request the weather in a location:

local httpService = game:GetService("HttpService")

local KEY = 'YOUR_API_KEY'
local LOCATION = 'CITY'

local URL = `http://api.weatherapi.com/v1/current.json?key={KEY}&q={LOCATION}`

local success, msg = pcall(function()
	
	local response = httpService:RequestAsync({
		Url = URL,
		Method = "POST",
	}
	)
	
	if response.Success then
		print(response.Body)
		-- Code to run when you get a successful response
	end
end)

if not success then
	print('HTTP request failed ', msg)
end

this will return something that looks like this:

{
"location":
	{
	"name":"Paris",
	"region":"Ile-de-France",
	"country":"France",
	"lat":48.87,
	"lon":2.33,"tz_id":"Europe/Paris",
	"localtime_epoch":1681230178,
	"localtime":"2023-04-11 18:22"},
	"current":{"last_updated_epoch":1681229700,
	"last_updated":"2023-04-11 18:15",
	"temp_c":13.0,
	"temp_f":55.4,
	"is_day":1,
	"condition":{
		"text":"Overcast",
		"icon":"//cdn.weatherapi.com/weather/64x64/day/122.png",
		"code":1009
	},
	"wind_mph":11.9,
	"wind_kph":19.1,
	"wind_degree":220,
	"wind_dir":"SW",
	"pressure_mb":1014.0,
	"pressure_in":29.94,
	"precip_mm":0.0,
	"precip_in":0.0,
	"humidity":54,
	"cloud":75,
	"feelslike_c":11.3,
	"feelslike_f":52.3,
	"vis_km":10.0,
	"vis_miles":6.0,
	"uv":3.0,
	"gust_mph":13.4,
	"gust_kph":21.6
	}
}

we can then write a function to extract useful data, for example cloud coverage.
from the weatherapi documentation we can see that "cloud" always returns cloud coverage in %.

we can then use a function like this to get the data from the returned string:

local function getCloudCoverage(str)
	return tonumber( str:match('"cloud":(%d+)') )
end

the final code would be:

local httpService = game:GetService("HttpService")

local KEY = 'YOUR_API_KEY'
local LOCATION = 'CITY'

local URL = `http://api.weatherapi.com/v1/current.json?key={KEY}&q={LOCATION}`

local function getCloudCoverage(str)
	return tonumber( str:match('"cloud":(%d+)') )
end

local success, msg = pcall(function()
	
	local response = httpService:RequestAsync({
		Url = URL,
		Method = "POST",
	}
	)
	
	if response.Success then
		local responseBody = response.Body
		
		local cloudCoverage = getCloudCoverage(responseBody)
		print(cloudCoverage) -- will return coverage in %
		-- for example you can change roblox's 'Cloud' instance density to the cloud coverage value you got
	end
end)

if not success then
	print('HTTP request failed ', msg)
end

Let me know if you have any more questions.

3 Likes

Hi Im confused on what an API key is and how I can get it and what do you mean by

You have to request an API key from the website, you need an API key to use their service.

1 Like

DATETIME
library

Using The DateTime Library, You Can Get The User’s LocalTime

However, This Will require use in a LocalScript,

IIRC Running it in a server script will get the real-time of the region the server is located in

You can use the code snipped below to do this.

Also, here is the DateTime Library, Here you can get intel on how to format local time as well as set based on area codes and timezones. DateTime | Roblox Creator Documentation

Code Snippet:

local IsLocalTime = true
local Is12HR = true
local AreaCode = "en-us"

function GetTime()
	local DT = DateTime.now()
	if IsLocalTime then
		return string.format("%s:%s:%s",Is12HR and DT:FormatLocalTime("hh",tostring(AreaCode)) or DT:FormatLocalTime("HH",tostring(AreaCode)),DT:FormatLocalTime("MM",tostring(AreaCode)),tostring(DT:FormatLocalTime("ss",tostring(AreaCode))))
	else
		return string.format("%s:%s:%s",Is12HR and DT:FormatUniversalTime("hh",tostring(AreaCode)) or DT:FormatUniversalTime("HH",tostring(AreaCode)),DT:FormatUniversalTime("MM",tostring(AreaCode)),tostring(DT:FormatLocalTime("ss",tostring(AreaCode))))
	end
end

task.spawn(function()
	while task.wait(1) do
		local DT = DateTime.now()
		local TC = DT:FormatLocalTime("A",tostring(AreaCode)) -- Get PM/AM
		warn(string.format("The Time Is %s %s",GetTime(),tostring(TC)))
	end
end)

which will return this:

image

Please feel free to reply if you have any questions and/or need help,

cheers -mrolivesgaming

Hi, if i live in Colombia, return the time of Colombia?