Project 1999

Project 1999 (/forums/index.php)
-   Off Topic (/forums/forumdisplay.php?f=19)
-   -   How to Calculate Age in Years, Months, and Days? (/forums/showthread.php?t=432958)

willey4983 07-15-2024 09:27 AM

How to Calculate Age in Years, Months, and Days?
 
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!

Tann 07-15-2024 04:35 PM

Excel, put a date in A1 then use:

Code:

=DATEDIF(A1, TODAY(), "Y") & " years, " & DATEDIF(A1, TODAY(), "YM") & " months, and " & DATEDIF(A1, TODAY(), "MD") & " days"

Duik 07-15-2024 05:05 PM

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...hrome&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:

Code:

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

Duik 08-03-2024 09:16 AM

^ Bot


All times are GMT -4. The time now is 05:22 AM.

Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.