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)
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.,
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