Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datetime.strptime(‘2017-01-12T14:12:06.000-0500’,'%Y-%m-%dT%H:%M:%S.%f%Z')

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?

like image 684
mrjextreme6 Avatar asked Jan 16 '17 21:01

mrjextreme6


People also ask

What does DateTime DateTime Strptime do?

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.

What does DateTime Strptime return?

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.

What does the P stand for in Strptime?

p for produce, str p time (strptime)-> string produces time.

What is Strptime and Strftime in Python?

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.


1 Answers

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
like image 164
Tagc Avatar answered Sep 30 '22 15:09

Tagc