Python Forum
Simple calculation using datetime
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple calculation using datetime
#3
In fact, the way you try to do this (with only the birthyear), will not always give the exact result. Because it will basically look to the age the subject will reach the particular year. But besides of that, the following code would do the trick right?

def get_age(birth_year):
    """
    returns the age based on current year
    """
    diff = date.today().year - birth_year
    return diff
To get the exact date you should also use the month and day in your calculation. You can simply calculate a datetime.date object with another datetime.date object. It will return a timedelta object.
Reply


Messages In This Thread
Simple calculation using datetime - by mapypy - Feb-06-2021, 09:13 AM
RE: Simple calculation using datetime - by Serafim - Feb-06-2021, 11:01 AM
RE: Simple calculation using datetime - by Jeff900 - Feb-06-2021, 07:34 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020