How to convert a python string to a date time
This article mainly shows you "how to convert python strings into date and time", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn how to convert python strings into date and time "this article.
1. Datetime.strptime is the main routine for parsing a string into a date time. It can handle a variety of formats, which are determined by the format string you provide.
From datetime import datetimedatetime_object = datetime.strptime ('Jun 1 2005 1Jun 33pm,' b% d% Y% iRu% M% p') 928
2. Use a third-party dateutil library, which can handle most date formats, including those you need to parse.
From dateutil import parserparser.parse ("Aug 28 1999 12:00AM") # datetime.datetime (1999, 8, 28, 0, 0)
It is more convenient than strptime to guess the correct format most of the time.
This is useful for writing tests because readability is more important than performance.
The above is all the content of the article "how to convert python strings to date and time". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!