我有这个json响应:
"dataDeNascimento":"1980-03-30T00:00:00",
我需要的是这样的响应:
1980年3月30日
我的代码部分如何表示此json:
response = session.get(url, headers=headers)
resp_json2 = response.json()
nascimento = resp_json2['dataDeNacimento']
如果你们能帮助我,我将不胜感激。
您可以使用datetime
fromdatetime
模块将日期字符串转换为datetime对象,然后再次将其转换为所需的日期字符串。
例如:
from datetime import datetime
date_ = "1980-03-30T00:00:00"
new_date = datetime.strptime(date_, '%Y-%m-%dT%H:%M:%S').strftime('%d/%m/%Y')
print(new_date)
输出:
30/03/1980
编辑:
正如@Mark Meyer所建议的,如果您正在使用,则Python >= 3.7
可以使用datetime.fromisoformat
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句