Python: how can I check whether an object is of type datetime.date?
Solution 1:
i believe the reason it is not working in your example is that you have imported datetime
like so :
from datetime import datetime
this leads to the error you see
In [30]: isinstance(x, datetime.date)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/<ipython-input-30-9a298ea6fce5> in <module>()
----> 1 isinstance(x, datetime.date)
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
if you simply import like so :
import datetime
the code will run as shown in all of the other answers
In [31]: import datetime
In [32]: isinstance(x, datetime.date)
Out[32]: True
In [33]:
Solution 2:
right way is
import datetime
isinstance(x, datetime.date)
When I try this on my machine it works fine. You need to look into why datetime.date
is not a class. Are you perhaps masking it with something else? or not referencing it correctly for your import?
Solution 3:
import datetime
d = datetime.date(2012, 9, 1)
print type(d) is datetime.date
> True