How can I properly format a date?

Hey! I am currently trying to display an account join date, but without the time. Here is what the raw date and time looks like: 2012-06-17T13:52:09.243Z
I was wondering if it was possible to get specific information from the string like the year, the month and the day alone. If anyone knows how I could format it, I would appreciate it. I was also wondering if it was possible to get the number of days since an account joined too.

Can’t you do date:sub(1, 10)? You would have to wait 7980 years to add an extra character to account for the year 10000.

If you don’t want to do that you can also do

date:match("%d+%-%d+%-%d+")
1 Like

Building up with what incapaz said, you can use string captures to returns parts of the matched string. This way you can get all the info you want in one line.

local example = "2012-06-17T13:52:09.243Z"
local day, time = string.match(example, "(%d+%-%d+%-%d+)T(%d+:%d+:%d+)Z")
--this will return the part which matches %d+%-%d+%-%d+, which is the date
--and the part which matches %d+:%d+:%d+, which is the time

You forgot the milliseconds part from the string, so if you just run this, it’ll return nil
Fix:

local example = "2012-06-17T13:52:09.243Z"
local day, time = string.match(example, "(%d+%-%d+%-%d+)T(%d+:%d+:%d+%.%d+)Z")

@OP: If you wish to not include the millseconds in the time, you can just put the pattern capturing the millisecond outside of the capture:

local example = "2012-06-17T13:52:09.243Z"
local day, time = string.match(example, "(%d+%-%d+%-%d+)T(%d+:%d+:%d+)%.%d+Z")
5 Likes

People have already answered how to use string patterns, and there are topics saying how to do it other way around, so I’m just going to answer your second question. You can use the Player’s AccountAge property, which will give the number of days since their account joined.

1 Like

Thanks.

30characters
30characters

Thank you. I didn’t know string:match existed.

Also, does the player have to be ingame in order to check the AccountAge property?

Yeah, they have to be in game unfortunately. You can use a web API if you need to get information about someone who’s not in-game, which is where the date parsing that others posted comes in.

1 Like

By the way, this question was asked before. This date format is ISO 8601.

While Rare’s solution works for the given time format, I do believe that the date format isn’t consistent as some of the parts of it can change (specifically the lettering), meaning that it could botch the overall fetch due to not being in exactly the given format.

The format proposed by the solution in the above thread accounts for the potential change with the T and the Z, which is as follows:

(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%+%-])(%d?%d?)%:?(%d?%d?)

For more information, see:

Not familiar with this kind of date format so please correct me if I’m wrong.

If I want to store the day, the month and the year as seperate variables, what can I do?

You can use the code below. This should work if the time format is consistent (ie. year-month-day)

local example = "2012-06-17T13:52:09.243Z"
local year, month, day, time = example:match("(%d+)%-(%d+)%-(%d+)T(%d+:%d+:%d+)%.%d+Z")

Thank you. Additionnaly, do you know of any resources that can help me learn about string:match?

It’s all string patterns: