如何在python中将日期时间字符串转换为日期时间格式,以便可以与其他日期进行比较?
string_date = "2013-09-28 20:30:55.78200"
abc = datetime.datetime.now()
if abc > string_date :
print True
Run Code Online (Sandbox Code Playgroud)
Vee*_*rac 173
特定格式strptime:
datetime.datetime.strptime(string_date, "%Y-%m-%d %H:%M:%S.%f")
#>>> datetime.datetime(2013, 9, 28, 20, 30, 55, 782000)
Run Code Online (Sandbox Code Playgroud)
Tho*_*zco 28
你应该使用datetime.datetime.strptime:
import datetime
dt = datetime.datetime.strptime(string_date, fmt)
Run Code Online (Sandbox Code Playgroud)
fmt将需要是您的字符串的适当格式.您可以在此处找到有关如何构建格式的参考.