Search code examples
pythondatetimepython-2.7timestrptime

Datetime from strings with microseconds


How to include microseconds to datetime, for example:

import datetime

dt = datetime.datetime.strptime('2010-08-30 15:02:55.730', '%Y-%m-%d %H:%M:%S')

>>> ValueError: unconverted data remains: .730

I want to have microseconds too. What should format string look like? What is the placeholder for microseconds?


Solution

  • dt = datetime.datetime.strptime('2010-08-30 15:02:55.730', '%Y-%m-%d %H:%M:%S.%f')
    

    you can look at:

    http://docs.python.org/2/library/datetime.html#strftime-strptime-behavior