-1

I have the following String representing a date time that I need to read into a valid Python3 datetime object. My best attempt so far:

from datetime import datetime

string_date = '2013-04-27T12:27:52.100Z'
in_memory_date = datetime.strptime(created, '???')

I'm struggling with the date format for this string (???). Can anybody point me in the right direction?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
hotmeatballsoup
  • 385
  • 6
  • 58
  • 136
  • that way: [strftime-strptime-behavior](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior) there a table what %... to put where, simply adjust it to your pattern – Patrick Artner Jun 25 '18 at 17:50
  • I couldn't find a dup, but you can simply use `'%Y-%m-%dT%H:%M:%S.%fZ'`. – Mazdak Jun 25 '18 at 17:53
  • This is the exact oppisite: how to create a ISO 8601 from datetime - in includes the parseformat string needed:[given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-format](https://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-format) – Patrick Artner Jun 25 '18 at 18:00

2 Answers2

2
datetime.datetime.strptime("2013-04-27T12:27:52.100Z", "%Y-%m-%dT%H:%M:%S.%fZ")
Mazdak
  • 105,000
  • 18
  • 159
  • 188
SamAtWork
  • 455
  • 5
  • 17
0

You can use to_datetime function from pandas. It is simpler.

pd.to_datetime(string_date)
Timestamp('2013-04-27 12:27:52.100000')
YOLO
  • 20,181
  • 5
  • 20
  • 40