Date and Time

#1

My code works fine. I am just calculating how many days old a person is. However my method for converting the user input into usable information is very tedious.

Is there a way to prompt the user only once and then extract the needed info in a much easier way?

``````from datetime import datetime
from datetime import date
from datetime import time

birth= raw_input("Birthday mm/dd/yyyy")
month = str(birth[0])+str(birth[1])
month=int(month)

day=str(birth[3])+str(birth[4])
day=int(day)

year=str(birth[6])+str(birth[7])+str(birth[8])+str(birth[9])
year=int(year)
today=date.today()
birthday=date(year,month,day)

print((today-birthday).days)``````

#2

Python lets us subtract one date from another and returns the number of days one is removed from the other.

``````from datetime import date
today = date.today()             # Jul 6

xmas = date(2016, 12, 25)
days_till_xmas = xmas - today
print days_till_xmas             # 172 days, 0:00:00``````

``````copernicus = date(1473,2,19)     # Nicholas' birthday
timedelta = today - copernicus
print timedelta                  # 218918 days, 0:00:00

galilei = date(1564,2,15)        # Galileo's birthday
timedelta = today - galilei
print timedelta                  # 165232 days, 0:00:00

newton = date(1643, 1, 4)        # Isaac's birthday
timedelta = today - newton
print timedelta                  # 136419 days, 0:00:00

hawking = date(1942, 1, 8)       # Stephen's birthday
timedelta = today - hawking
print timedelta                  # 27208 days, 0:00:00``````

Person's age in seconds
#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.