Hello, I want to make a feature like Flex Account Age, and I want to get players account age. I have used player.AccountAge and got the amount of days. How can I change the string to how many months and years it was?
Well, since the months have different day amounts, you may not get an exact number, but for a general month count, I would do local month = day/30. And to get years, local years = month/12.
I was thinking of trying it that way too, but was just seeing if there was other ways too.
Is there a way to even the numbers? They come out as decimal.
and also how would I get the days left over? Sorry if im asking to many questions.
To not make the numbers a decimal, use math.floor
An exact formula for getting the months and years is
Day / 30,417 = Months
Day / 365 = Years
Lol forgot its weeks, sorry Ill fix that
How would I add months? Add the weeks to it?
What do you mean? Getting the months?
Day / 30,417 = Months
Day / 7 = Weeks
Okay so I got months by dividing weeks by 4 which I might has done wrong but 8 months sound right.
Nothing is working all dates are wrong, still needing help.
Use the os
library, specifically os.date()
to get their join date, then calculate their age on months and years based on that.
print(os.date("%m %Y", 4209999900))
– this returns 05 2103
http://lua-users.org/wiki/OsLibraryTutorial
Ok mine prints out 07 1985 but how would this turn into years, months, days?
You can use DateTime’s which make it easy to do things in relation to time. Take this for example
local DT = DateTime.fromUnixTimestamp(os.time() - 86400 * player.AccountAge) --os.time() returns the seconds since the UNIX epoch and there's 86400 seconds in a day
local Month = tonumber(DT:FormatUniversalTime("MM", "en-us"))
local Year = tonumber(DT:FormatUniversalTime("YYYY", "en-us"))
print("Month: " .. Month .. " Year: " .. Year)
The first line gets the time the player’s account was created in a UnixTimestamp
The second and third lines then get the month and year the account was created.
But I want to get how long ago it was created, instead of when.
We don’t have to change much to achieve that. Instead of getting the difference we instead get the difference from the UNIX epoch
local DT = DateTime.fromUnixTimestamp(86400 * players.AccountAge)
local Days = tonumber(DT:FormatUniversalTime("DD", "en-us"))
local Months = tonumber(DT:FormatUniversalTime("MM", "en-us"))
local Years = tonumber(DT:FormatUniversalTime("YYYY", "en-us")) - 1970 -- 1970 is the unix epoch Year
print("Days: " .. Days .. " Months: " .. Months .. " Years: " .. Years)
Thanks for your help it works. Thank you again.
What if the month has 31 days or 28/29?