I have a variable and I need to know if it is a datetime object.
So far I have been using the following hack in the function to detect datetime object:
if 'datetime.datetime' in str(type(variable)):
print('yes')
But there really should be a way to detect what type of object something is. Just like I can do:
if type(variable) is str: print 'yes'
Is there a way to do this other than the hack of turning the name of the object type into a string and seeing if the string contains 'datetime.datetime'
?
I believe all the above answer will work only if date is of type datetime.datetime. What if the date object is of type datetime.time or datetime.date?
This is how I find a datetime object. It always worked for me. (Python2 & Python3):
Testing in Python2 or Python3 shell: