time data does not match format

Question:

I get the following error:

time data '07/28/2014 18:54:55.099000' does not match format '%d/%m/%Y %H:%M:%S.%f'

But I cannot see what parameter is wrong in %d/%m/%Y %H:%M:%S.%f ?

This is the code I use.

from datetime import datetime
time_value = datetime.strptime(csv_line[0] + '000', '%d/%m/%Y %H:%M:%S.%f')

I have added and removed the 000 but I get the same error.

Asked By: Stephane Rolland

||

Answers:

You have the month and day swapped:

'%m/%d/%Y %H:%M:%S.%f'

28 will never fit in the range for the %m month parameter otherwise.

With %m and %d in the correct order parsing works:

>>> from datetime import datetime
>>> datetime.strptime('07/28/2014 18:54:55.099000', '%m/%d/%Y %H:%M:%S.%f')
datetime.datetime(2014, 7, 28, 18, 54, 55, 99000)

You don’t need to add '000'; %f can parse shorter numbers correctly:

>>> datetime.strptime('07/28/2014 18:54:55.099', '%m/%d/%Y %H:%M:%S.%f')
datetime.datetime(2014, 7, 28, 18, 54, 55, 99000)
Answered By: Martijn Pieters

To compare date time, you can try this. Datetime format can be changed

from datetime import datetime

>>> a = datetime.strptime("10/12/2013", "%m/%d/%Y")
>>> b = datetime.strptime("10/15/2013", "%m/%d/%Y")
>>> a>b
False
Answered By: Basant Kumar

While the above answer is 100% helpful and correct, I’d like to add the following since only a combination of the above answer and reading through the pandas doc helped me:

2-digit / 4-digit year

It is noteworthy, that in order to parse through a 2-digit year, e.g. ’90’ rather than ‘1990’, a %y is required instead of a %Y.

Infer the datetime automatically

If parsing with a pre-defined format still doesn’t work for you, try using the flag infer_datetime_format=True, for example:

yields_df['Date'] = pd.to_datetime(yields_df['Date'], infer_datetime_format=True)

Be advised that this solution is slower than using a pre-defined format.

Answered By: whiletrue

I had a case where solution was hard to figure out. This is not exactly relevant to particular question, but might help someone looking to solve a case with same error message when strptime is fed with timezone information. In my case, the reason for throwing

ValueError: time data ‘2016-02-28T08:27:16.000-07:00’ does not match format ‘%Y-%m-%dT%H:%M:%S.%f%z’

was presence of last colon in the timezone part. While in some locales (Russian one, for example) code was able to execute well, in another (English one) it was failing. Removing the last colon helped remedy my situation.

Answered By: Anatoly Alekseev

No need to use datetime library. Using the dateutil library there is no need of any format:

>>> from dateutil import parser
>>> s= '25 April, 2020, 2:50, pm, IST'
>>> parser.parse(s)
datetime.datetime(2020, 4, 25, 14, 50)
Answered By: Amit Kumar

I had the exact same error but with slightly different format and root-cause, and since this is the first Q&A that pops up when you search for “time data does not match format”, I thought I’d leave the mistake I made for future viewers:

My initial code:

start = datetime.strptime('05-SEP-19 00.00.00.000 AM', '%d-%b-%y %I.%M.%S.%f %p')

Where I used %I to parse the hours and %p to parse ‘AM/PM’.

The error:

ValueError: time data ’05-SEP-19 00.00.00.000000 AM’ does not match format ‘%d-%b-%y %I.%M.%S.%f %p’

I was going through the datetime docs and finally realized in 12-hour format %I, there is no 00… once I changed 00.00.00 to 12.00.00, the problem was resolved.

So it’s either 01-12 using %I with %p, or 00-23 using %H.

Answered By: UdonN00dle

I had a similar error –

time data '01-07-2020' does not match format '%d%m%Y' (match)

I didn’t know that I have to use a hyphen in the format parameter. This worked for me –

df['Date'] = pd.to_datetime(df['Date'], format='%d-%m-%Y')
Answered By: AYUSH DAS

You have to just remove "/" from the String and put "," in it .. Boom

See the following code:

import datetime as dt
str = '01,01,2017'
datetime_value = dt.datetime.strptime(str,'%d,%m,%Y')
print(datetime_value) # prints: 2017-01-01 00:00:00
Answered By: Pro Root

I also had the same error, time data ’09/24/1997′ does not match format ‘%m-%d-%Y’

If your date is using / then also use / in your format, for example

’09/24/1997′ => ‘%m/%d/%Y’ using dashes will give you that error.

Answered By: Divine

Lets say we have datetime format string form with below format:

2018-01-31T09:24:31.488670+00:00 

We can convert this given datetime str to datetime object

import datetime 
        
DatetImeObj = datetime.datetime.strptime('2018-01-31T09:24:31.488670+00:00', '%Y-%m-%dT%H:%M:%S.%f%z') 

This will return datetime format with result:

2018-01-31 09:24:31.488670+00:00 
     

Now if you want to convert this date time in any format you can use any datetime function

finalDateTime = Datetime.datetime.strftime(DatetImeObj , ‘%Y-%m-%d %H:%M’)

output:

2018-01-31 09:24
Answered By: upendra

Try the capital "Y" if in case you are using the small "y"

for example:

Input: '2022-10-05 15:22:39'
datetime.datetime.strptime(date,'%Y-%m-%d %H:%M:%S')
Answered By: Gayathri Saranath
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.