I've been trying to convert this specific date format to a string in Python like so:
datetime.strptime(‘2017-01-12T14:12:06.000-0500’,'%Y-%m-%dT%H:%M:%S.%f%Z')
But it doesn't work.
What am I doing wrong?
Python DateTime – strptime() Function strptime() is another method available in DateTime which is used to format the time stamp which is in string format to date-time object.
Python time strptime() function The strptime() function in Python is used to format and return a string representation of date and time. It takes in the date, time, or both as an input, and parses it according to the directives given to it.
p for produce, str p time (strptime)-> string produces time.
strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.
The error was that you used %Z
instead of %z
. From the documentation, you should use %z
to match e.g. (empty), +0000, -0400, +1030
import datetime
result = datetime.datetime.strptime('2017-01-12T14:12:06.000-0500','%Y-%m-%dT%H:%M:%S.%f%z')
print(result)
Output
2017-01-12 14:12:06-05:00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With