Python 将日期时间格式应用于字符串
我有一个字符串格式的日期:Python 将日期时间格式应用于字符串,python,datetime,Python,Datetime,我有一个字符串格式的日期: extractdate='20200331' 我想将其转换为“2020年3月31日” 到目前为止,我有以下内容,但总是与datetime操作混淆: (str(datetime.strptime(extractdate,'%Y%m%d').day) + " " + str(datetime.strptime(extractdate,'%Y%m%d').month) + " " + str(datetime.strptime(extractdate,'%Y%m%d')
extractdate='20200331'
我想将其转换为“2020年3月31日”
到目前为止,我有以下内容,但总是与datetime
操作混淆:
(str(datetime.strptime(extractdate,'%Y%m%d').day) + " "
+ str(datetime.strptime(extractdate,'%Y%m%d').month) + " "
+ str(datetime.strptime(extractdate,'%Y%m%d').year))
这让我
'31 3 2020'
如何将
3
转换为Mar
?对于三月使用%B
,对于缩写的Mar使用%B
。您可以找到所有案例
使用%b,例如
datetime.strtime('20200331','%Y%m%d')。strftime('%d%b%Y')
。结帐,比你需要写问题的时间要少,所以;-)
datetime.strptime('20200331','%Y%m%d').strftime('%d %b %Y')