Search code examples
pythonstrptime

Python strptime is unable to parse date


When I try to run the following code, I keep getting a value error even though the date as per my knowledge follows the format string specified.

try:
  datetime.datetime.strptime('23-Jan-2017 15:30:00', '%d-%B-%Y %H:%M:%S')
except ValueError as e:
  print('Not mathcing')
  traceback.print_exc()

Where am I going wrong? The date and the format matches with the given format string, so what am I doing wrong?


Solution

  • You need a lowercase b for the month

    '%d-%B-%Y %H:%M:%S'
    

    should be

    '%d-%b-%Y %H:%M:%S'
    

    See here for a full list of formatting values

    %b  Month as locale’s abbreviated name. Sep
    %B  Month as locale’s full name.    September