Python Forum
Calculate AGE in years
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate AGE in years
#1
Hello

I want to try to calculate the age in years but in de output i get 1 day instead of 1 year

from datetime import datetime, date

class persoon:
  def __init__(self, naam, sekse, geboortedatum):
    self.naam = naam
    self.sekse = sekse
    self.geboortedatum = datetime.strptime(geboortedatum, '%d-%m-%Y').date()
  
  def getNaam(self):
    return self.naam

  def getGebDatum(self):
    return self.geboortedatum 
  
  def isVrouw(self):
    if self.sekse == "V":
      return True
    else:
      return False

  def isMan(self):
    if self.sekse == "M":
      return True
    else:
      return False   
    
  def leeftijd(self):
    today = date.today()
    age = (today - self.geboortedatum) // 365
    return age 

p1 = persoon("John", "M", "24-04-2022")

print(p1.getNaam(), p1.leeftijd())
Output
John 1 day, 0:00:00

What I want is

John 1 Year
Gribouillis write Apr-24-2023, 08:03 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
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,809 Oct-14-2022, 03:20 PM
Last Post: betinajessen
  Problem with code to calculate weekday for leap years! Event 2 2,857 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 9,996 Mar-26-2018, 04:44 AM
Last Post: PyMan
  human years to dog years jhall710 7 11,457 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