Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how can I check whether an object is of type datetime.date?

People also ask

Is datetime a type in Python?

Date and datetime are an object in Python, so when you manipulate them, you are actually manipulating objects and not string or timestamps. date – An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Its attributes are year, month and day.

How do you check if a string is a date in Python?

You can check if a string is a date using the Python strptime() function from the datetime module. strptime() takes a string and a date format, and tries to create a datetime object. If the string matches the given string format, then the datetime object is created. If not, a ValueError occurs.

What is the type of date and time in Python?

There are two types of date and time objects. The types are naïve and the aware. In the naïve object, there is no enough information to unambiguously locate this object from other date-time objects.


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]: 

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?


import datetime
d = datetime.date(2012, 9, 1)
print type(d) is datetime.date

> True

According to documentation class date is a parent for class datetime. And isinstance() method will give you True in all cases. If you need to distinguish datetime from date you should check name of the class

import datetime

datetime.datetime.now().__class__.__name__ == 'date' #False
datetime.datetime.now().__class__.__name__ == 'datetime' #True
datetime.date.today().__class__.__name__ == 'date' #True
datetime.date.today().__class__.__name__ == 'datetime' #False

I've faced with this problem when i have different formatting rules for dates and dates with time


If your existing code is already relying on from datetime import datetime, you can also simply also import date

from datetime import datetime, timedelta, date
print isinstance(datetime.today().date(), date)

In Python 3.8.4 it can be checked that the method with isinstance will fail when checking if a datetime is whether a date or a datetime as both checks will give True.

    >>> from datetime import datetime, date
    >>> mydatetime = datetime.now()
    >>> mydate = mydatetime.date()
    >>> isinstance(mydatetime, datetime)
    True
    >>> isinstance(mydatetime, date)
    True

This is due to the fact that datetime is a subclass of date and as it is explained in this other answer:

an instance of a derived class is an instance of a base class, too

Therefore, when distinguishing between datetime and date, type should be used instead:

    >>> type(mydatetime) == date
    False
    >>> type(mydate) == date
    True
    >>> type(mydate) == datetime
    False
    >>> type(mydatetime) == datetime
    True

In Python 3.5, isinstance(x, date) works to me:

>>> from datetime import date
>>> x = date(2012, 9, 1)
>>> type(x)
<class 'datetime.date'>
>>> isinstance(x, date)
True
>>> type(x) is date
True