计算年龄的Python程序
给定 y/m/d 格式的出生日期,编写一个Python程序以查找当前年龄(以年为单位)。
例子:
Input : 1997/2/3
Output : 21 years (for present year i.e 2018)
Input : 2010/12/25
Output : 8 years (for present year i.e 2018)
方法#1:
以年为单位查找当前年龄的简单方法是找出当前年份与出生年份之间的差异。从这里参考 Naive 方法。
方法 #2:使用 datetime 模块
Python提供了 datetime 模块来处理Python中所有与 datetime 相关的问题。使用日期时间,我们可以通过从当前年份减去出生年份来找到年龄。除此之外,我们还需要关注出生月份和生日。为此,我们检查当前月份和日期是否小于出生月份和日期。如果是,从年龄中减去 1,否则为 0。
Python3
# Python3 code to calculate age in years
from datetime import date
def calculateAge(birthDate):
today = date.today()
age = today.year - birthDate.year -
((today.month, today.day) <
(birthDate.month, birthDate.day))
return age
# Driver code
print(calculateAge(date(1997, 2, 3)), "years")
Python3
# Python3 code to calculate age in years
from datetime import date
def calculateAge(born):
today = date.today()
try:
birthday = born.replace(year = today.year)
# raised when birth date is February 29
# and the current year is not a leap year
except ValueError:
birthday = born.replace(year = today.year,
month = born.month + 1, day = 1)
if birthday > today:
return today.year - born.year - 1
else:
return today.year - born.year
# Driver code
print(calculateAge(date(1997, 2, 3)), "years")
Python3
# Python3 code to calculate age in years
from datetime import date
def calculateAge(birthDate):
days_in_year = 365.2425
age = int((date.today() - birthDate).days / days_in_year)
return age
# Driver code
print(calculateAge(date(1997, 2, 3)), "years")
输出:
21 years
方法#3:高效的日期时间方法
上述方法不处理特殊情况,即出生日期为 2 月 29 日且当年不是闰年。这种情况必须作为例外提出,因为生日的计算可能不准确。此方法包括针对此异常的 try 和 catch。
Python3
# Python3 code to calculate age in years
from datetime import date
def calculateAge(born):
today = date.today()
try:
birthday = born.replace(year = today.year)
# raised when birth date is February 29
# and the current year is not a leap year
except ValueError:
birthday = born.replace(year = today.year,
month = born.month + 1, day = 1)
if birthday > today:
return today.year - born.year - 1
else:
return today.year - born.year
# Driver code
print(calculateAge(date(1997, 2, 3)), "years")
输出:
21 years
方法#4:使用除法
在这种方法中,我们计算从出生日期到当前日期的日期数。将日期数除以一年中的天数,即 365.2425。
Python3
# Python3 code to calculate age in years
from datetime import date
def calculateAge(birthDate):
days_in_year = 365.2425
age = int((date.today() - birthDate).days / days_in_year)
return age
# Driver code
print(calculateAge(date(1997, 2, 3)), "years")
输出:
21 years