%不支持的操作数类型:“ datetime.timedelta”和“ int”

Zak*_*aks 0 python datetime

我的python代码如下:

if ( delta % 24 == 0):
    print "ONE DAY "
Run Code Online (Sandbox Code Playgroud)

它给出错误为TypeError:%不支持的操作数类型:“ datetime.timedelta”和“ int”

delta is of type datetime.timedelta
Run Code Online (Sandbox Code Playgroud)

请分享您的输入以解决此错误。根据项目要求使用pyton 2.7

gsa*_*ras 5

DateTime 不支持取模,因此会看到错误。

不过,这种对日期时间的Python模支持


此外,您可以强制转换第二个操作数,以便消除错误:

if ( ( d % timedelta(minutes = 24) ) == 0):
     print("ONE DAY")
Run Code Online (Sandbox Code Playgroud)

在Python 3.6.1中有效。

编辑:

这不适用于Python 2.7.0,已针对OP的问题进行了编辑。在这种情况下,这可能会有所帮助:在python中操作DateTime和TimeDelta对象