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

后端 未结 7 1189
既然无缘
既然无缘 2020-12-24 10:16

I have tried a few obvious options but none of them works:

In [150]: x
Out[150]: datetime.date(2012, 9, 1)

In [151]: type(x)
Out[151]: datetime.date

In [15         


        
7条回答
  •  借酒劲吻你
    2020-12-24 11:06

    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)
    

提交回复
热议问题