根据身份证年龄计算

年龄计算

def calculate_age(input_born):
    '''
    : input_born: string, len=8
    '''
    import datetime

    y_born = input_born[0:4]
    m_born = input_born[4:6]
    d_born = input_born[6:8]
    
    born = datetime.date(int(y_born), int(m_born), int(d_born))
    today = datetime.date(2018,5,10)
    
    born_days = born - datetime.date(born.year-1, 12, 31)  #减去上一年最后一天,可得解
    target_days = today - datetime.date(today.year-1, 12, 31)
    sub_days = target_days - born_days
    sub_days = sub_days.days
    
#     try:
#         birthday = born.replace(year=today.year)
#     except ValueError:
#         # raised when birth date is February 29 
#         # and the current year is not a leap year
#         birthday = born.replace(year=today.year, day=born.day-1)
        
    if today > born:
        years = today.year-born.year
        if sub_days >= 0:
            if sub_days>=0 and sub_days<183:
                return years
            else:
                # sub_days in range(183,366,1):
                return years+1
        
        else:
            sub_days = abs(sub_days)
            if sub_days>=0 and sub_days<183:
                return years
            else:
                return years+1
    else:
        print('error_date')
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值