zoukankan      html  css  js  c++  java
  • python 根据生日计算年龄 sqlalchemy根据身份证号计算生日 性别

    import datetime
    
    
    def calculate_age(birth_s='20181215'):
        birth_d = datetime.datetime.strptime(birth_s, "%Y%m%d")
        today_d = datetime.datetime.now()
        birth_t = birth_d.replace(year=today_d.year)
        if today_d > birth_t:
            age = today_d.year - birth_d.year
        else:
            age = today_d.year - birth_d.year - 1
    
        print('出生日期:%s' % birth_d)
        print('今年生日:%s' % birth_t)
        print('今天日期:%s' % today_d)
        print('如果今天日期大于今年生日,今年-出生年=年龄')
        print('如果今天日期不大于今年生日,今年-出生年-1=年龄')
        print('年龄:%s' % age)
        return age
    
    
    if __name__ == '__main__':
        print(calculate_age('20171216'))

    放入sqlalchemy模型定义中,id_Num是身份证号,其中6-14位是出生日期。

        @property
        def age(self):
            if len(self.id_Num) > 14:
                birth_s = self.id_Num[6:14]
                birth_d = datetime.datetime.strptime(birth_s, "%Y%m%d")
                today_d = datetime.datetime.now()
                birth_t = birth_d.replace(year=today_d.year)
                if today_d > birth_t:
                    age = today_d.year - birth_d.year
                else:
                    age = today_d.year - birth_d.year - 1
            else:
                age = 0
            return age

    引申一下,有身份证以后,性别也可以计算出来

    @property
        def gender(self):
            if len(self.IdNum) > 17:
                tag = self.IdNum[16:17]
                if (int(tag) % 2) == 0:
                    result = ''
                else:
                    result = ''
            else:
                result = '未知'
            return result

    加一个excel算性别

    =IF(MOD(MID(D4,17,1),2),"男","女")

  • 相关阅读:
    Sqli-labs Less-47 order by后的注入
    Sqli-labs Less-46 order by后的注入
    Sqli-labs Background-9 order by后的injection
    Sqli-labs Less-45 堆叠注入
    jsp,servlet知识点
    jsp页面编码不统一可能会出问题
    jsp页面找不到,jsp页面乱码
    BZOJ 2843: 极地旅行社 lct splay
    2018/3/23 省选模拟赛
    bzoj 4573: [Zjoi2016]大森林 lct splay
  • 原文地址:https://www.cnblogs.com/jackadam/p/12041838.html
Copyright © 2011-2022 走看看