Search code examples
pythonpython-3.xstrptime

strptime failing to parse given a correct format


I've written down the formatting that strptime should use to parse my string into datetime, but it kept on failing without a clear reason as to why.

I have a date/time string that I wanted to parse: '2019-06-17T05:35:30' I gave it the following format: '%y-%m-%dT%H:%M:%S' It apparently doesn't match.

I tried replacing the 'T' with a space using the replace method and changing the format accordingly to no avail.

tmpTime = eval(data.columns[2])['sleep'][0]['startTime'][:19] #Excluded 
#milliseconds by trimming whatever is after the '.', including the '.' itself.
dt = datetime.strptime(tmpTime, '%y-%m-%dT%H:%M:%S')

---------------- Jupyter Error Display ----------------------

ValueError                                Traceback (most recent call last)
<ipython-input-13-a379ef67d5ba> in <module>
     20 tmpTime = eval(data.columns[2])['sleep'][0]['startTime'][:19]
     21 #tmpTime = tmpTime.replace("T"," ")
---> 22 dt = datetime.strptime(tmpTime, '%y-%m-%dT%H:%M:%S')

~\AppData\Local\Programs\Python\Python37\lib\_strptime.py in _strptime_datetime(cls, data_string, format)
    575     """Return a class cls instance based on the input string and the
    576     format string."""
--> 577     tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    578     tzname, gmtoff = tt[-2:]
    579     args = tt[:6] + (fraction,)

~\AppData\Local\Programs\Python\Python37\lib\_strptime.py in _strptime(data_string, format)
    357     if not found:
    358         raise ValueError("time data %r does not match format %r" %
--> 359                          (data_string, format))
    360     if len(data_string) != found.end():
    361         raise ValueError("unconverted data remains: %s" %

ValueError: time data '2019-06-17T05:35:30' does not match format '%y-%m-%dT%H:%M:%S'

Supposed to parse correctly into a datetime object, fails.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-a379ef67d5ba> in <module>
     20 tmpTime = eval(data.columns[2])['sleep'][0]['startTime'][:19]
     21 #tmpTime = tmpTime.replace("T"," ")
---> 22 dt = datetime.strptime(tmpTime, '%y-%m-%dT%H:%M:%S')

~\AppData\Local\Programs\Python\Python37\lib\_strptime.py in _strptime_datetime(cls, data_string, format)
    575     """Return a class cls instance based on the input string and the
    576     format string."""
--> 577     tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    578     tzname, gmtoff = tt[-2:]
    579     args = tt[:6] + (fraction,)

~\AppData\Local\Programs\Python\Python37\lib\_strptime.py in _strptime(data_string, format)
    357     if not found:
    358         raise ValueError("time data %r does not match format %r" %
--> 359                          (data_string, format))
    360     if len(data_string) != found.end():
    361         raise ValueError("unconverted data remains: %s" %

ValueError: time data '2019-06-17T05:35:30' does not match format '%y-%m-%dT%H:%M:%S'

Solution

  • Your string format is in the wrong case for year .This should be signified as '%Y'. eg:

    tmpTime =  '2019-06-17T05:35:30'
    dt = datetime.strptime(tmpTime, '%Y-%m-%dT%H:%M:%S')
    >>>dt
    datetime.datetime(2019, 6, 17, 5, 35, 30)
    

    You can find the correct formats for datetime parsing here