import datetime

def calculate_age(birth_s=''):
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(''))

放入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),"男","女")

python 根据生日计算年龄 sqlalchemy根据身份证号计算生日 性别的更多相关文章

  1. js中如何通过身份证号计算出生日期和年龄

    在html中有如下标签 身份证号:<input type="text" id="Gra_IDCard" onChange="IDCardChan ...

  2. sql server 根据身份证号计算出生日期和年龄的存储过程

    我这边有一个业务,需要客户填写身份证号,自动计算他的出生日期和年龄 在sql中,具体的存储过程实现是这样的: /******************************************** ...

  3. 教你一招:根据身份证号计算出生年月和年龄 text函数和mid函数混用 datedif函数和today函数混用

    在电子表格Excel中,使用text函数和mid函数混用,根据身份证号计算出生年月: =text(mid(A2,,),"0!/00!/00") #0!/00!/00 为日期的格式# ...

  4. SQL语句通过身份证号计算年龄

    SQL语句通过身份证号计算年龄 1.截取身份证号上的出生日期 身份证一般为18位数和15位数 18位数身份证的第7-10位数是出生年份,第11-14位数是出生月日,所以18位身份证的年龄计算如下 su ...

  5. oracle 根据身份证号计算出生日期

      1.情景展示 如何根据身份证号推算出出生日期? 2.解决方案 --根据身份证号计算出生日期 SELECT DECODE(LENGTH(ID_CARD), 18, SUBSTR(ID_CARD, 7 ...

  6. 从身份证号提取生日并更新到生日字段中的SQL语句

    1:根据身份证号 更新 生日字段 SQL update 学生信息 ,)+,)+,) 2:根据身份证号 更新 性别字段 SQL update 学生信息 set 性别='男' and substring( ...

  7. sqlalchemy 模型中添加根据身份证号计算性别和年龄

    class Users(Base, BaseMixin): username = Column(String(24)) id_Num = Column(String(18) ) # 身份证号码 @pr ...

  8. EXCEL计算根据当前时间和身份证号计算准确年龄

    假设身份证号在A2单元格 =IF(MONTH(NOW())<MONTH(DATE(MID(A2,7,4),MID(A2,11,2),MID(A2,13,2))),INT(YEAR(NOW())- ...

  9. JS通过身份证号获取生日、年龄、性别

    <script> function IdCard(UUserCard,num){ if(num==1){ //获取出生日期 birth=UUserCard.substring(6, 10) ...

随机推荐

  1. 线程的等待与唤醒,实现if...else里面的值交互依次输出

    线程通信原理图: 资源类: package com.yonyou.sci.gateway.exec.threadnet; public class Resource { String name; St ...

  2. What is the difference between Reactjs and Rxjs?--React is the V (View) in MVC (Model/View/Controller).

    This is really different, React is view library; and Rxjs is reactive programming library for javasc ...

  3. CF1102D-Balanced Ternary String-(贪心)

    http://codeforces.com/problemset/problem/1102/D 题意: 有n个字符,只能为012,现要通过变换让012的数量相等,并且使字典序最小. 解题: 由样例可以 ...

  4. 使用viper 进行golang 应用的配置管理

    viper 是一个强大的golang 配置管理包,支持多种配置格式内容的读取,同时支持读取key/value 存储的数据 而且不只是读取内容 ,同时也包含了,配置的写入操作. 以下是一个简单的demo ...

  5. shell习题训练

    shell习题训练 求2个数之和 计算1-100的和 将一目录下所有的文件的扩展名改为bak 编译当前目录下的所有.c文件: 打印root可以使用可执行文件数,处理结果: root's bins: 2 ...

  6. OOO的CSS

    应ooo要求 寻找他手写一千年的css的继承人 html { background:#f7f7f7 url(images/bg-pattern.jpg) } body { margin:; paddi ...

  7. git 中文文件名乱码

    git 默认中文文件名是 \xxx\xxx 等八进制形式,是因为 对0x80以上的字符进行quote. 只需要设置core.quotepath设为false,就不会对0x80以上的字符进行quote. ...

  8. Technocup 2020 Elimination Round 3题解

    传送门 \(A\) 曲明连sb模拟不会做,拖出去埋了算了 //quming #include<bits/stdc++.h> #define R register #define fi fi ...

  9. LINK : fatal error LNK1181: cannot open input file 'glew32.lib' error: command 'C:\\Program Files (

    下载 库文件 参考: https://stackoverflow.com/questions/53355474/kivent-installation-fatal-error-lnk1181-cant ...

  10. shell 编写进度条

    test.sh #!/bin/bash i= bar='' label=("|" "/" "-" "\\") ] do ...