Issue
How do I convert the following string to a datetime
object?
"Jun 1 2005 1:33PM"
Solution
datetime.strptime
parses an input string in the user-specified format into a timezone-naive datetime
object:
>>> from datetime import datetime
>>> datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
datetime.datetime(2005, 6, 1, 13, 33)
To obtain a date
object using an existing datetime
object, convert it using .date()
:
>>> datetime.strptime('Jun 1 2005', '%b %d %Y').date()
date(2005, 6, 1)
Links:
strftime.org format string cheatsheet
Notes:
strptime
= "string parse time"strftime
= "string format time"
Answered By - Patrick Harrington
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.