How would I swap dates from US to UK format?

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

This is the code that fetches it:

Date_Created.Text = "Created - ".. string.sub(MarketplaceService:GetProductInfo(Game_ID).Created , 1 , 10)

Figured it out! never even saw “string.split” very useful to know!

local Filtered_Date = string.sub(MarketplaceService:GetProductInfo(Game_ID).Created , 1 , 10)
Filtered_Date = string.split(Filtered_Date,"-")

Date_Created.Text = "Created - ".. Filtered_Date[3].. "-".. Filtered_Date[2].. "-".. Filtered_Date[1]

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

1 Like

That’s a nice way of doing it actually! might use that instead.

Edit: I will be using that instead, Allows me to easily localize, Thank you! :blush:

1 Like

Here’s my way of doing it:

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.