#1
|
|||
|
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! | ||
#2
|
|||
|
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"
__________________
< Knights Who Say Ni >
Qeynos questing and leveling (all quests nerfed) | Off the beaten path 24-40. | ||
#3
|
|||
|
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. | ||
#4
|
|||
|
If only there was a tool that could tell you exactly how to do this
| ||
#5
|
|||
|
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.
__________________
Apophis is closest to earth on 2029 April the 13th (a friday) lol
| ||
#6
|
|||
|
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
__________________
Apophis is closest to earth on 2029 April the 13th (a friday) lol
| ||
#7
|
|||
|
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
__________________
Apophis is closest to earth on 2029 April the 13th (a friday) lol
| ||
Last edited by magnetaress; 07-15-2024 at 07:32 PM..
|
#8
|
|||
|
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") I hope this is what you're looking for, or at least something similar. | ||
#9
|
|||
|
math
| ||
#10
|
|||
|
^ Bot
| ||
Thread Tools | |
Display Modes | |
|
|