hutool.core.util.idcard

Module Contents

Classes

IdcardUtil

身份证工具类

API

class hutool.core.util.idcard.IdcardUtil

身份证工具类

_PROVINCE_CODES = None
_WEIGHT_FACTORS = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
_CHECK_CODES = '10X98765432'
_ID_CARD_18_PATTERN = 'compile(...)'
_ID_CARD_15_PATTERN = 'compile(...)'
static is_valid_idcard(idcard: str) bool

校验身份证号是否有效(支持15位和18位)

参数:

idcard – 身份证号

返回:

是否有效

static is_valid_card18(idcard: str) bool

校验18位身份证

校验规则: 1. 格式校验(17位数字 + 第18位数字或X/x) 2. 省份代码校验 3. 出生日期校验(合法的YYYYMMDD) 4. 校验码计算验证(加权求和 mod 11)

参数:

idcard – 18位身份证号

返回:

是否有效

static is_valid_card15(idcard: str) bool

校验15位身份证

参数:

idcard – 15位身份证号

返回:

是否有效

static get_birth(idcard: str) str

获取出生日期字符串,如 “19900101”

参数:

idcard – 身份证号

返回:

出生日期字符串(YYYYMMDD格式),无效则返回空字符串

static get_age(idcard: str) int

根据身份证号获取年龄

参数:

idcard – 身份证号

返回:

年龄,无效则返回-1

static get_age_by_birth(birth_date: datetime.date) int

根据出生日期获取年龄

参数:

birth_date – 出生日期

返回:

年龄

static get_gender(idcard: str) str

获取性别

身份证第17位(18位身份证)或第15位(15位身份证): 奇数为男性,偶数为女性

参数:

idcard – 身份证号

返回:

“M”(男)或 “F”(女),无效则返回空字符串

static get_province(idcard: str) str

获取省份

参数:

idcard – 身份证号

返回:

省份名称,无效则返回空字符串

static convert15to18(idcard15: str) str

15位身份证转18位

转换规则: 1. 在第6位后插入”19”(年份补全) 2. 计算第18位校验码

参数:

idcard15 – 15位身份证号

返回:

18位身份证号,无效输入返回空字符串

static get_year_by_id_card(idcard: str) int

获取身份证号中的出生年份

参数:

idcard – 身份证号(15位或18位)

返回:

出生年份,无效输入返回 -1

static get_month_by_id_card(idcard: str) int

获取身份证号中的出生月份

参数:

idcard – 身份证号(15位或18位)

返回:

出生月份(1-12),无效输入返回 -1

static get_day_by_id_card(idcard: str) int

获取身份证号中的出生日

参数:

idcard – 身份证号(15位或18位)

返回:

出生日(1-31),无效输入返回 -1

static convert_18_to_15(idcard18: str) str

18位身份证转15位(去掉年份前两位和校验码)

参数:

idcard18 – 18位身份证号

返回:

15位身份证号,无效输入返回空字符串

static is_valid_tw_card(idcard: str) bool

校验台湾身份证号

台湾身份证号格式:1位字母 + 9位数字 校验规则:字母对应数字 + 9位数字加权求和,末位为校验码

参数:

idcard – 台湾身份证号

返回:

是否有效

static is_valid_hk_card(idcard: str) bool

校验香港身份证号

香港身份证号格式:1-2位字母 + 6位数字 + 1位校验码(括号内数字或字母)

参数:

idcard – 香港身份证号

返回:

是否有效

static get_birth_date(idcard: str) datetime.date

获取出生日期对象

参数:

idcard – 身份证号

返回:

出生日期,无效输入返回 None

static get_city_code(idcard: str) str

获取身份证号中的城市编码(地址码后4位)

参数:

idcard – 身份证号

返回:

城市编码,无效输入返回空字符串

static get_district_code(idcard: str) str

获取身份证号中的区县编码(地址码后2位)

参数:

idcard – 身份证号

返回:

区县编码,无效输入返回空字符串

static get_idcard_info(idcard: str) dict

获取身份证号的完整信息

参数:

idcard – 身份证号

返回:

信息字典,包含 province, city_code, gender, birth_date, age, is_valid

static hide(idcard: str) str

隐藏身份证号中间部分

18位身份证:保留前6位和后4位,中间用****代替 15位身份证:保留前4位和后4位,中间用****代替

参数:

idcard – 身份证号

返回:

脱敏后的身份证号