View Full Version : How to Calculate Age in Years, Months, and Days?
willey4983
07-15-2024, 09:27 AM
I'm working on a project that requires calculating the exact age of individuals in years, months, and days. I've tried a few different methods but I'm not confident that I'm doing it correctly.
Can anyone provide a reliable formula or method to accurately calculate someone's age from their birthdate to the current date? Any code examples or step-by-step explanations would be greatly appreciated! Thank you!
Excel, put a date in A1 then use:
=DATEDIF(A1, TODAY(), "Y") & " years, " & DATEDIF(A1, TODAY(), "YM") & " months, and " & DATEDIF(A1, TODAY(), "MD") & " days"
Homework question?
1st post from user.
Seems to be an odd request on an elf sim forum.
Here is a hint for anyone. If you have tried a few different methods mention and list them showing where you think you went wrong.
People do not know how to ask questions. No wonder the interwebs is full of nitwits.
Smoofers
07-15-2024, 05:10 PM
If only there was a tool that could tell you exactly how to do this
magnetaress
07-15-2024, 06:57 PM
Do you have api access or library access to time and date functions? Most languages already handle this.
If you're making your own functions that requires an underlying understanding of time and dates that I cannot provide.
I'm 9999% sure Javascript handles things like this in its basic library and you just got to learn what functions to call and feed it the relevant data.
magnetaress
07-15-2024, 07:28 PM
https://www.javatpoint.com/cpp-date-and-time
https://www.google.com/search?q=time+and+date+functions+c%2B%2B&rlz=1C1ONGR_enUS1010US1010&oq=time+and+date+functions+c%2B%2B&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIICAEQABgWGB4yDQgCEA AYhgMYgAQYigUyDQgDEAAYhgMYgAQYigUyDQgEEAAYhgMYgAQY igUyCggFEAAYgAQYogTSAQg0MTcwajBqN6gCALACAA&sourceid=chrome&ie=UTF-8
obviously JS, perl, sh, and python all have their own... dunno if 'ruby' is its own thing i been outa the biz a long time
magnetaress
07-15-2024, 07:29 PM
you'll probably have to convert time to something more tangible, do ur maths, then convert ur strings back
like convert it all to seconds, add, do ur maths, then feed ur seconds back into one of the time strings
so like take current date and old date and convert both to seconds, do maths, then convert seconds back to days/hrs/months etc back to something more human readable
ONE of those functions may or may not do that for u, or part of it, u got to dig around in the documentations
a3n5ql2
07-17-2024, 03:50 PM
First, extract the birthdate and current date components (year, month, day). Then, calculate the difference in years, months, and days. Adjust the calculation if the day of the birth month is greater than the current day.
Here's a Python code example:
from datetime import date
def calculate_age(birthdate):
today = date.today()
years = today.year - birthdate.year
months = today.month - birthdate.month
days = today.day - birthdate.day
# Adjust if current day is less than birthdate day
if days < 0:
months -= 1
# Calculate days in previous month
last_month = today.month - 1 if today.month > 1 else 12
last_month_year = today.year if today.month > 1 else today.year - 1
days_in_last_month = (date(last_month_year, last_month + 1, 1) - date(last_month_year, last_month, 1)).days
days += days_in_last_month
# Adjust if current month is less than birthdate month
if months < 0:
years -= 1
months += 12
return years, months, days
# Example usage
birthdate = date(1990, 5, 15) # Replace with the actual birthdate
age = calculate_age(birthdate)
print(f"Age: {age[0]} years, {age[1]} months, {age[2]} days")
In this code, we first import the date class from the datetime module to handle date objects. Then, we define the calculate_age function, which takes a birthdate as input. We get today's date using date.today() and calculate the difference in years, months, and days between today’s date and the birthdate. Adjustments are made if the current day is less than the birthdate day by subtracting one month and adding the correct number of days from the previous month. Similarly, if the current month is less than the birthdate month, we subtract one year and add the correct number of months.
I hope this is what you're looking for, or at least something similar.
Rimitto
08-03-2024, 06:35 AM
math
loramin
08-03-2024, 10:42 AM
Ya'll (including the bot) are talking to a bot.
So are you when u engage rimitto. But you said if it is interesting and not attacking its all good!
Also. The advice i gave was not for the bot. Just generally good questioning skills if seeking help on a forum.
If i say that once and nobody else says it, the bot will not rate it as it is statistically insignificant. The best sort of insignificant.
loramin
08-04-2024, 11:18 AM
Just to be clear, there's two kinds of "bot" poster.
The first always has low/no post count and a generic username (eg. MattSmith123), because they were created by a literal " bot" (script). Their posts may have come from ChatGPT prompts, or a human-compiled set. Either way, the bot wants to make money through ads (it's probably pestering countless similar forums the same way), and it's only posting this "normal" post to rack up enough post count to post an image ad.
Then, there's Rimitto. We know there is a real human behind that account, because half the time they actually post. The other half they post what seems like ChatGPT-generated responses, but I'm unclear if those were even automated, or if Rimitto copy/pasted them from ChatGPT. Either way, their motivation doesn't seem to be to disrupt conversation and sell Russian cam girls ... it just seems like they're a bored nerd messing around.
vBulletin® v3.8.11, Copyright ©2000-2025, vBulletin Solutions Inc.