129

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.

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169

11 Answers11

151

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)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
107

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.

whiletrue
  • 10,500
  • 6
  • 27
  • 47
23

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)
Amit Kumar
  • 619
  • 7
  • 10
  • 1
    For me it gives an UnknownTimezoneWarning in Python 3.7 with IST. It works with CET. – Arpad Horvath -- Слава Україні Apr 28 '20 at 06:38
  • 2
    Just this `from dateutil import parser;parser.parse('25 April, 2020, 2:50, pm, IST')`, I use the python3-dateutil 2.7.3-3 package in Debian 10. I haven't checked the latest version with pip. – Arpad Horvath -- Слава Україні Apr 28 '20 at 18:22
  • Well answered. This is the best approach without mentioning the format. +1 for that. – Swarup Aug 25 '20 at 14:37
  • Simple solution, worked perfectly. I was struggling with a client that would sometimes send me xx:xx or xx:xx:xx for time and I would get errors. – Jeff Bluemel Mar 31 '21 at 22:35
  • Using parser to automatically parse the datetime string to datetime is effortless, indeed. However, I found it's not perfect and **error prone**. For example, if you try this example, you will found it did in the **wrong** way: `parser.parse("2023:03:24 18:00:23")` it gives you `datetime.datetime(2023, 4, 11, 18, 0, 23)`, which is clearly wrong! So be attention with using it. – Seraph Apr 11 '23 at 03:12
6

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.

Anatoly Alekseev
  • 2,011
  • 24
  • 27
3

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
Basant Kumar
  • 129
  • 1
  • 3
2

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.

UdonN00dle
  • 723
  • 6
  • 28
2

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')
AYUSH DAS
  • 21
  • 2
2

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.

Divine
  • 71
  • 3
  • 3
2

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
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
upendra
  • 339
  • 4
  • 20
1

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')
-5

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
Leo
  • 1,508
  • 13
  • 27
  • 2
    Hello. Welcome to Stackoverflow. With your suggestion, you modify both the input string, and also the format to be parsed in the input string. As such, I believe it does not answer the question. – Stephane Rolland Apr 17 '21 at 10:49