What's the equivalent type in types module for datetime? Example:
import datetime
import types
t=datetime.datetime.now()
if type(t)==types.xxxxxx:
do sth
I didn't find the relevent type in types module for the datetime type; could any one help me?
datetime in Python is the combination between dates and times. The attributes of this class are similar to both date and separate classes. These attributes include day, month, year, minute, second, microsecond, hour, and tzinfo.
The DateTime value type represents dates and times with values ranging from 00:00:00 (midnight), January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.) in the Gregorian calendar. Time values are measured in 100-nanosecond units called ticks.
date Objects. A date object represents a date (year, month and day) in an idealized calendar, the current Gregorian calendar indefinitely extended in both directions. January 1 of year 1 is called day number 1, January 2 of year 1 is called day number 2, and so on. 2 class datetime. date (year, month, day)
>>> type(t)
<type 'datetime.datetime'>
>>> type(t) is datetime.datetime
True
Is that the information you're looking for? I don't think you'll be able to find the relevant type within the types
module since datetime.datetime
is not a builtin type.
Edit to add: Another note, since this is evidently what you were looking for (I wasn't entirely sure when I first answered) - type checking is generally not necessary in Python, and can be an indication of poor design. I'd recommend that you review your code and see if there's a way to do whatever it is you need to do without having to use type checking.
Also, the typical (canonical? pythonic) way to do this is with:
>>> isinstance(t, datetime.datetime)
True
See also: Differences between isinstance() and type() in python, but the main reason is that isinstance()
supports inheritance whereas type()
requires that both objects be of the exact same type (i.e. a derived type will evaluate to false
when compared to its base type using the latter).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With