Created
December 2, 2016 02:17
-
-
Save shahri23/1804a3acb7ffb58a1ec8f1eda304af1a to your computer and use it in GitHub Desktop.
Python code to show age in years, months, days, ...seconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Figure out your age | |
import datetime | |
currentDate = datetime.datetime.now() | |
deadline= input ('Plz enter your date of birth (mm/dd/yyyy) ') | |
deadlineDate= datetime.datetime.strptime(deadline,'%m/%d/%Y') | |
print (deadlineDate) | |
daysLeft = deadlineDate - currentDate | |
print(daysLeft) | |
years = ((daysLeft.total_seconds())/(365.242*24*3600)) | |
yearsInt=int(years) | |
months=(years-yearsInt)*12 | |
monthsInt=int(months) | |
days=(months-monthsInt)*(365.242/12) | |
daysInt=int(days) | |
hours = (days-daysInt)*24 | |
hoursInt=int(hours) | |
minutes = (hours-hoursInt)*60 | |
minutesInt=int(minutes) | |
seconds = (minutes-minutesInt)*60 | |
secondsInt =int(seconds) | |
print('You are {0:d} years, {1:d} months, {2:d} days, {3:d} hours, {4:d} \ | |
minutes, {5:d} seconds old.'.format(yearsInt,monthsInt,daysInt,hoursInt,minutesInt,secondsInt)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I modify the script to display days only after going over 31 days? ("32 days", "33 days" etc instead of "1 month, 1 day", "1 month, 2 days"?