%不支持的操作数类型:“ datetime.timedelta”和“ int” [英] unsupported operand type(s) for %: 'datetime.timedelta' and 'int'

查看:145
本文介绍了%不支持的操作数类型:“ datetime.timedelta”和“ int”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的python代码如下:

My python code is as below:

if ( delta % 24 == 0):
    print "ONE DAY "

它给出错误,错误为TypeError:%:'datetime不支持的操作数类型。 timedelta'和'int'

It gives error as TypeError: unsupported operand type(s) for %: 'datetime.timedelta' and 'int'

delta is of type datetime.timedelta

请分享您的输入以纠正此错误。
由于项目要求而使用pyton 2.7

Please share your inputs to fix this error. Using pyton 2.7 due to project requirement

推荐答案

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

DateTime doesn't support modulo, thus the error you see.

尽管存在对日期时间的Python模支持

此外,您可以转换第二个操作数,这样错误就消失了:

Moreover, you could cast the second operand, so that the error goes away:

if ( ( d % timedelta(minutes = 24) ) == 0):
     print("ONE DAY")

在Python 3.6.1中有效。

which works in Python 3.6.1.

编辑:

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

This won't work for Python 2.7.0, which was edited to OP's question. In that case, this may help: Manipulating DateTime and TimeDelta objects in python.

这篇关于%不支持的操作数类型:“ datetime.timedelta”和“ int”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆