Python Forum
Calculate AGE in years
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate AGE in years
#2
You can't do this. A normal year has 364 days and a leap year has 365 days.
Calculate instead the years and not the timedelta.

import datetime
import calendar

# today = datetime.date.today()
today = datetime.date(2000, 2, 29)
bday = datetime.date(1984, 2, 29)

years = today.year - bday.year

# correct the bday, if it's the 29th february and today is not a leap yaar
if bday.month == 2 and bday.day == 29 and not calendar.isleap(today.year):
    bday = datetime.date(bday.year, 3, 1)

# check if today earlier than birthday
if (today.month, today.day) < (bday.month, bday.day):
    years -= 1
    # today it's not the birthday, so one year lesser
print(years)
Improvement of your class:
class Persoon:
    def __init__(self, naam, sekse, geboortedatum):
        self._naam = naam
        self._sekse = sekse
        self._geboortedatum = datetime.strptime(geboortedatum, "%d-%m-%Y").date()

    @property
    def naam(self):
        return self._naam

    @property
    def geb_datum(self):
        return self._geboortedatum

    @property
    def vrouw(self):
        return self._sekse == "V"

    @property
    def man(self):
        return self._sekse == "M"

    def leeftijd(self, today=None):
        today = today or date.today()
        years = today.year - self.geb_datum.year
        geboortedatum = self.geb_datum

        # leap fix 29 feb.
        if (
            geboortedatum.month == 2
            and geboortedatum.day == 29
            and not isleap(today.year)
        ):
            geboortedatum = date(geboortedatum.year, 3, 1)

        if (today.month, today.day) < (self.geb_datum.month, self.geb_datum.day):
            years -= 1

        return years


p1 = Persoon("John", "M", "29-2-1980")
# 2023 is not a leap year
print(p1.naam, p1.leeftijd(date(2023, 3, 1)))
The properties are read-only.
leeftijd() is still a method, but now you can also set today (better for testing).
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Calculate AGE in years - by Kessie1971 - Apr-24-2023, 07:31 AM
RE: Calculate AGE in years - by DeaD_EyE - Apr-24-2023, 09:46 AM
RE: Calculate AGE in years - by Kessie1971 - Apr-24-2023, 05:07 PM
RE: Calculate AGE in years - by DeaD_EyE - Apr-24-2023, 05:28 PM
RE: Calculate AGE in years - by Kessie1971 - Apr-24-2023, 06:05 PM
RE: Calculate AGE in years - by deanhystad - Apr-24-2023, 06:22 PM
RE: Calculate AGE in years - by Kessie1971 - Apr-24-2023, 06:36 PM
RE: Calculate AGE in years - by deanhystad - Apr-24-2023, 07:20 PM
RE: Calculate AGE in years - by Kessie1971 - Apr-24-2023, 07:23 PM
RE: Calculate AGE in years - by buran - Apr-25-2023, 06:35 AM
RE: Calculate AGE in years - by DeaD_EyE - Apr-25-2023, 08:37 AM
RE: Calculate AGE in years - by buran - Apr-25-2023, 08:55 AM
RE: Calculate AGE in years - by Kessie1971 - Apr-25-2023, 05:00 PM
RE: Calculate AGE in years - by deanhystad - Apr-25-2023, 05:33 PM
RE: Calculate AGE in years - by Kessie1971 - Apr-25-2023, 05:44 PM
RE: Calculate AGE in years - by Kessie1971 - Apr-25-2023, 05:56 PM
RE: Calculate AGE in years - by deanhystad - Apr-25-2023, 07:15 PM
RE: Calculate AGE in years - by deanhystad - Apr-26-2023, 03:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tracking leap.py years for gregorian calendar (Exercism org) Drone4four 11 3,918 Oct-14-2022, 03:20 PM
Last Post: betinajessen
  Problem with code to calculate weekday for leap years! Event 2 2,890 Dec-15-2018, 05:13 PM
Last Post: Event
  python age calculator need to find the number of years before they turn 100 not using orangevalley 4 10,056 Mar-26-2018, 04:44 AM
Last Post: PyMan
  human years to dog years jhall710 7 11,526 May-08-2017, 05:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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