Python:如何检查对象是否为 datetime.date 类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16151402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: how can I check whether an object is of type datetime.date?
提问by
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 [152]: isinstance(x, datetime.date)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-152-9a298ea6fce5> in <module>()
----> 1 isinstance(x, datetime.date)
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
In [153]: x is datetime.date
Out[153]: False
In [154]: type(x) is datetime.date
Out[154]: False
What is the right way of doing this?
这样做的正确方法是什么?
采纳答案by olly_uk
i believe the reason it is not working in your example is that you have imported datetimelike so :
我相信它在您的示例中不起作用的原因是您已datetime像这样导入:
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]:
回答by cmd
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.dateis not a class. Are you perhaps masking it with something else? or not referencing it correctly for your import?
当我在我的机器上尝试这个时,它工作正常。你需要研究为什么datetime.date不是一个类。你可能用别的东西来掩盖它吗?或未正确引用它以进行导入?
回答by Simon Steinberger
import datetime
d = datetime.date(2012, 9, 1)
print type(d) is datetime.date
> True
回答by Dmitry B.
If your existing code is already relying on from datetime import datetime, you can also simply also import date
如果您现有的代码已经依赖from datetime import datetime,您也可以简单地导入date
from datetime import datetime, timedelta, date
print isinstance(datetime.today().date(), date)
回答by fedorqui 'SO stop harming'
In Python 3.5, isinstance(x, date)works to me:
在 Python 3.5 中,isinstance(x, date)对我有用:
>>> from datetime import date
>>> x = date(2012, 9, 1)
>>> type(x)
<class 'datetime.date'>
>>> isinstance(x, date)
True
>>> type(x) is date
True
回答by Roman Kazakov
According to documentationclass dateis a parent for class datetime. And isinstance()method will give you Truein all cases. If you need to distinguish datetimefrom dateyou should check name of the class
根据文档classdate是class的父级datetime。在所有情况下,isinstance()方法都会给你True。如果您需要区分datetime的date,你应该检查类的名称
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
当我对日期和日期随时间有不同的格式规则时,我遇到了这个问题

