A module script to reliably get UTC time!

Hey Everyone!
So this is quite a simple script, but it’s very easy to read and understand.
This script is a module script that returns the current UTC time.

It returns it in this format:
2nd January 2022 - 20:53

So it is very readable in the output and the actual code.

So here is the module script

local function check(x1)
	local str = x1
	local var = "th"
	local sec = string.sub(str,-1,-1)
	if sec == "1" then
		var = "st"
	elseif sec == "2" then
		var = "nd"
	elseif sec == "3" then
		var = "rd"
	end

	return(str..var)
end

local exchange = {
	[1]	= "January",
	[2] = "February",
	[3] = "March",
	[4] = "April",
	[5] = "May",
	[6] = "June",
	[7] = "July",
	[8] = "August",
	[9] = "September",
	[10] = "October",
	[11] = "November",
	[12] = "December"
}

local module = {}

module.utc = function()
	local utc_time = os.date("!*t")

	local day = utc_time.day
	local month = utc_time.month
	local year = utc_time.year
	local hour = utc_time.hour
	local min = utc_time.min

	if #tostring(hour) == 1 then
		hour = ("0"..hour)
	end

	if #tostring(min) == 1 then
		min = ("0"..min)
	end


	local newday = check(tostring(day))
	local newmonth = exchange[month]

	local final = (newday.." "..newmonth.. " "..year.." - "..hour..":"..min)
	return final
end

return module

And of course, if you wanted to call it you would do this,

local module = require(game:GetService("ReplicatedStorage").UTCscript)
local utcTime = module.utc()
print(utcTime)

And that’s it,
Thanks for reading

3 Likes

Instead adding the function to a table just return the module as a function, there’s no point in doing module.[function] if there’s only one function. I’d change it to just simply module().

local function check(x1)
	local str = x1
	local var = "th"
	local sec = string.sub(str,-1,-1)
	if sec == "1" then
		var = "st"
	elseif sec == "2" then
		var = "nd"
	elseif sec == "3" then
		var = "rd"
	end

	return(str..var)
end

local exchange = {
	[1]	= "January",
	[2] = "February",
	[3] = "March",
	[4] = "April",
	[5] = "May",
	[6] = "June",
	[7] = "July",
	[8] = "August",
	[9] = "September",
	[10] = "October",
	[11] = "November",
	[12] = "December"
}
return function()
	local utc_time = os.date("!*t")

	local day = utc_time.day
	local month = utc_time.month
	local year = utc_time.year
	local hour = utc_time.hour
	local min = utc_time.min

	if #tostring(hour) == 1 then
		hour = ("0"..hour)
	end

	if #tostring(min) == 1 then
		min = ("0"..min)
	end


	local newday = check(tostring(day))
	local newmonth = exchange[month]

	local final = (newday.." "..newmonth.. " "..year.." - "..hour..":"..min)
	return final
end

The module was meant to hold more features than just UTC time, for example, current time, etc
So it would be cleaner to have all time-related stuff from one module, rather than multiple, os.time() multiple times.,

Feels like this reinvents the wheel for DateTime.

print(DateTime.now():FormatUniversalTime("LLL", "en-ca"))
-- Results in: January 3, 2022 12:11 AM

A comment on your check function; there are some things you can omit in this function such as redeclaring the parameter as a local variable. This is a whole lot longer than what you have but just an example of how I might’ve written that instead:

local NUMBER_SUFFIXES = {"st", "nd", "rd"}

-- Pass a number type to addSuffix
local function addSuffix(number: number | string): string
	-- Change the type of number into a string if it isn't
	if not typeof(number) == "string" then number = tostring(number) end

	-- Find the last digit in the number string
	local lastNumber = tonumber(string.match(number, "%d$"))
	
	-- Set the suffix
	-- Fake ternary will result in nil concatenation error
	local suffix = if lastNumber == 1 then "st"
		elseif lastNumber == 2 then "nd"
		elseif lastNumber == 3 then "rd"
		else "th"

	-- Attach a suffix or the default "th" suffix and return as a string
	return number .. suffix
end

-- Test it out?
for i = 0, 10 do
	print(addSuffix(i))
end
1 Like

Sorry if im dumb, but can’t you just do:

os.date("%X")

to get the local time. And if you do it on the server(while in game) you’d get UTC? Not sure if im right tho.

I originally wanted the code to get me the time and date, in that specific format.
2nd January 2022 - 20:53

But then I thought I might as well post it here to see what people have to say.

Thanks for your in-depth response though!

Yeah but I also wanted the hyphen and date, thinking about it, it would have been shorter to concentrate both, os.date("%X") and os.date("%x")

1 Like

Sorry to bump this post (found it in my suggested)

For final you should use string.format() as it is more reliable and shorter than concatenation.

1 Like