如何使用python将日期转换为2019年2月20日inot 20-02-2019格式?

时间:2019-12-03 15:27:46

标签: python

我具有以下日期格式 enter image description here

我尝试使用以下代码使用pandas或datetime包将其转换为DateTime格式 import datetime as dt df_huawei['Date'] = df_huawei['Date'].apply(lambda x:dt.datetime.strptime(x,'%d%b%Y:%H:%M:%S.%f')),但出现以下错误

ValueError: time data 'May 31, 2019' does not match format '%d%b%Y:%H:%M:%S.%f

我已经阅读了许多解决方案,但我的日期格式未包含在所有讨论中,请您帮忙

2 个答案:

答案 0 :(得分:2)

import datetime as dt
dttmp=dt.datetime.strptime("Feb 20,2019","%b %d,%Y")
dtstr=dttmp.strftime("%d-%m-%Y")
dtstr

答案 1 :(得分:0)

您的格式错误。 请参阅strptime文档:

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

这将适合您的情况:

import datetime as dt dt.datetime.strptime('May 31, 2019', '%b %d, %Y')