Once I have fetched and filtered the date created of a game I am left with “2023-07-08”, I know I need to use string formatting but I couldn’t really figure out how id do it, maybe it’s easy and I’m missing something.
Basically I want to do this:
2023-07-08 → 08-07-2023
Glad you figured it out, heres the code I made anyway.
local usdate = "2023-07-08"
local year, month, day = usdate:match("(%d+)-(%d+)-(%d+)")
local ukdate = day .. "-" .. month .. "-" .. year
print(ukdate) -- 08-07-2023
function ToUK(date: string): string
local p = date:split("-")
return string.format("%s-%s-%s", p[3], p[2], p[1])
end
print(ToUK("2023-07-08")) --> 08-07-2023
Here’s another way of doing it that allows you to pass multiple dates at once:
--n tells the function how many dates to replace, nil is all
function ToUK(date: string, n: number?): string
--the reason it's stored in a variable is to only keep the first tuple value(the result)
local str = date:gsub("(%d+)-(%d+)-(%d+)", "%3-%2-%1", n)
return str
end
print(ToUK("2023-07-08 2023-05-10")) --> 08-07-2023 10-05-2023