How to get the player's account age (in days, like on player profiles)

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.

Any help is appreciated, thanks.

2 Likes

Depending on the language, there’s probably a date library. You can import that date into the library and extract the amount of days from it.

What language are you doing this in?

I’m using Lua, just trying to find out the days since an account is created for a Roblox game.

Previous Post

Roblox Lua? Ooooo, that’s a tricky one.

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.

1 Like

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?

1 Like

You can try parsing the json date

local epoch = os.time{year=1970, month=1, day=1, hour=0}
function parse_json_date(json_date)
	local year, month, day, hour, minute, seconds, offsetsign, offsethour, offsetmin = json_date:match("(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%+%- ])(%d?%d?)%:?(%d?%d?)")
	local timestamp = os.time{year = year, month = month, day = day, hour = hour, min = minute, sec = seconds} - epoch
	local offset = 0
	if offsetsign ~= 'Z' then
		offset = tonumber(offsethour) * 60 + tonumber(offsetmin)
		if offsetsign == "-" then offset = -offset end
	end
	return timestamp - offset * 60
end

local timestamp = parse_json_date('2019-09-26T10:49:19.627Z')

local Date = os.date("*t", timestamp)

print(Date)

image
Source: lua ISO 8601 datetime parser - https://repl.it/IQuI/5 · GitHub

1 Like

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

9 Likes

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.

2 Likes

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.

1 Like

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.

1 Like