I want to know how to find player’s account age in terms of days since they have joined. The API I could find (https://users.roblox.com/docs#!/Users/get_v1_users_userId) only gives the creation date in this format: “created”: “2019-09-26T10:49:19.627Z” and I don’t prefer that.
Note: I couldn’t use Player.AccountAge because the player is not in-game.
I don’t think Roblox has a date library, so this is going to suck a lot. Date libraries are the hardest libraries to maintain. Maybe you can find an open source one written in Lua and use it?
Otherwise, I would suggest finding some external API that gives you this information. You might need to write your own API for this potentially if you don’t want to mess with making your own date library.
EDIT: Oh wait!
EDIT2: Turns out there is a DateTime library now on roblox. Check out below.
Thanks, that would work. I think now with this, I have to deal with string formatting. Could you please show me an example code how I could convert “created”: “2019-09-26T10:49:19.627Z” to format it to dd/mm/yy?
If you’re using this API on a Roblox game server and you’re not trying to find the player’s age in days off Roblox, then you’re in luck. What a time to be working with dates after DateTime was added.
The DateTime class provides a method for converting ISO 8601 dates (the format you’re seeing) into DateTime objects which can then further be used for creating date-based systems. Gone are the days of needing to find some weird string pattern to be able to work with this format properly.
The first thing you will need are two DateTime objects, one that represents your ISO date and one that represents the current moment in time.
local createdTime = DateTime.fromIsoDate("2019-09-26T10:49:19.627Z")
local currentTime = DateTime.now()
As you cannot perform arithmetic operations against two DateTime objects you will need to access their UnixTimestamp properties (or Millis if resolution matters, but in the case of this system it does not) which will give you the amount of seconds between these dates and the Unix Epoch. The sum of these two numbers will give you the account’s age in seconds, which you then divide by 86400 to get the account’s age (recall: 86400 seconds = 1 day).
local createdTime = DateTime.fromIsoDate("2019-09-26T10:49:19.627Z").UnixTimestamp
local currentTime = DateTime.now().UnixTimestamp
local accountAgeDays = (currentTime - createdTime)/86400
For reference, you can compare the result of your math/DateTime arithmetics with your own account age. For example, my AccountAge property is 4003. Using this code with my account’s creation date ISO 2010-02-28T14:30:45.17Z also gives back 4003 (with decimals that can be cut off with a math function, preferably floor).
How is this different from currentTime - createdTime? DateTime is useful for parsing the ISO string, sure, but I don’t see what it’s doing for you here.
They aren’t different. I just happened to write the original post with that and it didn’t exactly occur to me then that I was already subtracting two Unix Timestamps only to put it right back into another DateTime object and get back the same Unix Timestamp.
Thanks for reminding me, I’ve made changes accordingly.
Sorry for being late to this thread, but you can simply use player.AccountAge, which is a property that provides the number days since the account was created.