Skip to content

Instantly share code, notes, and snippets.

@shahri23
Created December 2, 2016 02:17
Show Gist options
  • Save shahri23/1804a3acb7ffb58a1ec8f1eda304af1a to your computer and use it in GitHub Desktop.
Save shahri23/1804a3acb7ffb58a1ec8f1eda304af1a to your computer and use it in GitHub Desktop.
Python code to show age in years, months, days, ...seconds
# 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))
@pauladesh
Copy link

pauladesh commented Aug 14, 2020

It is showing in negative. For example:
You are -25 years, -11 months, -23 days, -19 hours, -13 minutes, -18 seconds old.

To resolve it, write this in line number 8.

daysLeft = currentDate - deadlineDate

@soumi-learner
Copy link

Can I use abs() to resolve negative no?

@jacob-goodall
Copy link

Yes
`

Figure out your age

import datetime
currentDate = datetime.datetime.now()

deadline= input ('Please enter your date of birth (dd/mm/yyyy): ')
deadlineDate= datetime.datetime.strptime(deadline,'%d/%m/%Y')
print (deadlineDate)
daysLeft = deadlineDate - currentDate
print(daysLeft)

years = ((daysLeft.total_seconds())/(365.242243600))
years = abs(years)
yearsInt=int(years)

months=(years-yearsInt)*12
months = abs(months)
monthsInt=int(months)

days=(months-monthsInt)*(365.242/12)
days = abs(days)
daysInt=int(days)

hours = (days-daysInt)*24
hours = abs(hours)
hoursInt=int(hours)

minutes = (hours-hoursInt)*60
minutes = abs(minutes)
minutesInt=int(minutes)

seconds = (minutes-minutesInt)*60
seconds = abs(seconds)
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))

`

@nizarfadlan
Copy link

Can I use abs() to resolve negative no?

Use

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 = currentDate - deadlineDate
print(daysLeft)

years = ((daysLeft.total_seconds())/(365.242243600))
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))

@TomaszSojkowski
Copy link

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"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment