Advertisement
elena1234

Calculate age from datetime ( Python )

Jan 14th, 2022
1,702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.37 KB | None | 0 0
  1. from datetime import date
  2. import pandas as pd
  3.  
  4. born_inString = input('What year were you born? ')
  5. born_inDate = pd.to_datetime(born_inString)
  6.  
  7. def calculate_age(born_inDate):
  8.     today = date.today()
  9.     return today.year - born_inDate.year - ((today.month, today.day) < (born_inDate.month, born_inDate.day))
  10.  
  11. print(f'You are {calculate_age(born_inDate)} years old.')
  12.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement