0

I have been searching for a clarified answer on this. I have read many threads and other websites about using datetime, but how can I build a simple program that prompts a user for two certain dates, and then calculate the difference between those dates? I am stuck on figuring out how to get the month and days.

This is what I have so far:

print ("How old will you be when you graduate?")
print ("If you want to know, I'll have to ask you a few questions.")

name = input('What is your name? ')

bYear = int(input("What year were you born? "))

print ("Don't worry. Your information is safe with me.")

bMonth = int(input("What month were you born? "))
bDay = int(input("How about the day you were born? "))

print ("Fantastic. We're almost done here.")

gYear = int(input("What year do you think you'll graduate? "))
gMonth = int(input("What about the month? "))
gDay = int(input("While you're at it, give me the day too. "))


age = gYear - bYear

if bMonth > gMonth:
    age = age - 1
    aMonth = 
if bMonth == gMonth and bDay > gDay:
    age = age - 1

print(name, 'you will be', age, 'years old by the time you graduate. Thanks for the information.')

I am just beginning to use Python, but it's seems pretty simple just coming in. So, for example I want to prompt the user for the year they were born, the month they were born, and the day they were born. Then I want to prompt the user for the year they will graduate, the month they will graduate, and the day they will graduate. Then I want the program to calculate the difference between those dates and output the age of the person in years, months, and days.

For example

BORN AUGUST 23, 1995
WILL GRADUATE: JUNE 11, 2018
AGE AT GRADUATION : 22 YEARS 9 MONTHS AND 18 DAYS
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

0

You can use the datetime and dateutil modules. datetime is in the Python standard library, however, dateutil is 3rd party - it can be installed with pip.

from datetime import date
from dateutil.relativedelta import relativedelta

birth_date = date(year=1992, month=12, day=21)
graduation_date = date(year=2016, month=9, day=28)
diff = relativedelta(graduation_date, birth_date)

>>> print(diff)
relativedelta(years=+23, months=+9, days=+7)
>>> print('AGE AT GRADUATION : {0.years} YEARS {0.months} MONTHS AND {0.days} DAYS'.format(diff))
AGE AT GRADUATION : 23 YEARS 9 MONTHS AND 7 DAYS
mhawke
  • 84,695
  • 9
  • 117
  • 138