如何在python中将字符串日期转换为日期时间格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19068269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert a string date into datetime format in python?
提问by PythonEnthusiast
How do I convert a a string of datetime into datetime format in python so that it can be compared with another date?
如何在python中将日期时间字符串转换为日期时间格式,以便可以与另一个日期进行比较?
string_date = "2013-09-28 20:30:55.78200"
abc = datetime.datetime.now()
if abc > string_date :
print True
采纳答案by Veedrac
The particular format for strptime
:
的特定格式strptime
:
datetime.datetime.strptime(string_date, "%Y-%m-%d %H:%M:%S.%f")
#>>> datetime.datetime(2013, 9, 28, 20, 30, 55, 782000)
回答by Thomas Orozco
You should use datetime.datetime.strptime
:
你应该使用datetime.datetime.strptime
:
import datetime
dt = datetime.datetime.strptime(string_date, fmt)
fmt
will need to be the appropriate format for your string. You'll find the reference on how to build your format here.