如何在python中将日期时间字符串转换为日期时间格式,以便可以与其他日期进行比较?如何在python中将字符串日期转换为日期时间格式?
string_date = "2013-09-28 20:30:55.78200"
abc = datetime.datetime.now()
if abc > string_date :
print True
如何在python中将日期时间字符串转换为日期时间格式,以便可以与其他日期进行比较?如何在python中将字符串日期转换为日期时间格式?
string_date = "2013-09-28 20:30:55.78200"
abc = datetime.datetime.now()
if abc > string_date :
print True
为strptime
的特定格式:
datetime.datetime.strptime(string_date, "%Y-%m-%d %H:%M:%S.%f")
#>>> datetime.datetime(2013, 9, 28, 20, 30, 55, 782000)
你应该使用datetime.datetime.strptime
:
import datetime
dt = datetime.datetime.strptime(string_date, fmt)
fmt
将需要为您的字符串适当的格式。你会找到reference on how to build your format here。
你的意思是说'DT = datetime.datetime.strptime(string_date, “YYYY-MM-DD%H:%M%:%S”)'? – PythonEnthusiast
@ user1162512该格式不正确,请检查我的更新答案。 –
不适用于'dt = datetime.datetime.strptime(string_date,“YYYY-MM-DD%H:%M%:%S”)''。说格式无效 – PythonEnthusiast
%f支持哪些平台? –
@JonathanLeaders我建议你问一个单独的问题。 – Veedrac