-1

I am having trouble converting the date time string 2017-10-15T03:15:04.293 into a Date object in python, simply because I can't figure out the format.

I have the code line: x.strftime('%Y-%m-%d'), which accurately gives me the date itself, but lacks the time obviously. When trying to modify the string formatter, I am getting confused by the T, and the last decimal .293 (from the example above).

Can someone help me understand what these values mean in this timestamp, and what the proper x.strftime('<WHAT GOES HERE ???>') might be?

Brett
  • 11,637
  • 34
  • 127
  • 213
  • 3
    *"what these values mean in this timestamp "* - https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior. See also many duplicates on parsing ISO 8601 datetimes, e.g. https://stackoverflow.com/q/127803/3001761 – jonrsharpe Oct 15 '17 at 16:12
  • I posted my answer as @jonrsharpe updated his comment. My answer will work, but I agree with him that this should really be marked a dupe. – bnaecker Oct 15 '17 at 16:36

1 Answers1

0

The numbers after the . are microseconds. You can parse it using the datetime module, like so:

from datetime import datetime
datetime.strptime('2017-10-15T03:15:04.293', '%Y-%m-%dT%H:%M:%S.%f')

The format looks like ISO-8601, which is a widely-used standard. But it appears that the datetime module doesn't yet provide a way to parse this without providing the format-string yourself.

bnaecker
  • 6,152
  • 1
  • 20
  • 33