python实现从输入的18位身份证号,以类似于2001年09月12日的形式输出
时间: 2023-05-29 10:03:16 浏览: 154
可以使用Python标准库中的datetime模块来实现:
```python
import datetime
id_card = input("请输入18位身份证号码:")
birth_year = int(id_card[6:10])
birth_month = int(id_card[10:12])
birth_day = int(id_card[12:14])
birth_date = datetime.date(birth_year, birth_month, birth_day)
print("出生日期:{}".format(birth_date.strftime("%Y年%m月%d日")))
```
运行示例:
```
请输入18位身份证号码:420923199001010101
出生日期:1990年01月01日
```
相关问题
python从键盘交互式输入一个人的18位身份证号,以类似于“2001年09月12日”的形式输出该人的出生日期。
身份证号码中的前6位是出生地信息,接下来8位是出生日期信息,最后4位是顺序和校验码。因此,通过截取身份证号码中的第7位到第14位,即可获取到该人的出生日期信息。
下面是代码实现:
```python
id_card = input("请输入18位身份证号:")
# 获取出生日期信息并处理成年月日
birth_year = id_card[6:10]
birth_month = id_card[10:12]
birth_day = id_card[12:14]
birth_date = birth_year + "年" + birth_month + "月" + birth_day + "日"
print("该人的出生日期为:" + birth_date)
```
示例输出:
```
请输入18位身份证号:110108199611230044
该人的出生日期为:1996年11月23日
```
注意:以上代码仅演示了如何从身份证号码中获取出生日期信息,并以类似于“2001年09月12日”的形式输出,仅供参考。实际开发过程中,应加上身份证号码合法性校验等操作。
python从键盘交互式输入一个人的18位的身份证号,以类似于“2001年09月12日”的形式输出该人的出生日期。
可以使用Python的datetime模块来进行日期格式化的操作,请尝试以下代码:
```python
import datetime
id_card = input("请输入18位身份证号:")
birth_year = id_card[6:10]
birth_month = id_card[10:12]
birth_day = id_card[12:14]
birth_date = datetime.datetime.strptime("{}-{}-{}".format(birth_year, birth_month, birth_day), '%Y-%m-%d')
print("该人的出生日期为:{}".format(birth_date.strftime("%Y年%m月%d日")))
```
以上代码中,我们首先从键盘输入18位身份证号,并根据身份证中的6到14位来提取出生年月日信息。接着使用datetime模块的strptime方法进行日期的格式化操作,最终将其转换为“年-月-日”的形式。最后,再使用strftime方法将日期格式化为“xxxx年xx月xx日”的形式进行输出。