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

时间:2017-09-01 11:55:39

标签: python datetime

我的python代码如下:

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

它给出了错误TypeError:%支持的操作数类型:' datetime.timedelta'和' int'

delta is of type datetime.timedelta

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

2 个答案:

答案 0 :(得分:5)

DateTime不支持模数,因此您看到错误。

Python modulo support for datetime存在。

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

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

适用于Python 3.6.1。

编辑:

这对于编译为OP问题的Python 2.7.0不起作用。在这种情况下,这可能有所帮助:Manipulating DateTime and TimeDelta objects in python

答案 1 :(得分:0)

阅读const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.pushNotification = functions.database.ref('/messages/{pushId}').onWrite( event => { console.log('Push notification event triggered'); var message1 = event.data.val(); if (!message1) { return; } var valueObject = message1.messageText; console.log('Push notification event triggered '+valueObject); const payload = { notification: { title:'Chat app', body: valueObject, }, }; const options = { priority: "high", timeToLive: 0 * 0 * 0 }; return admin.messaging().sendToTopic("pushNotifications", payload, options).then(function(response) { console.log("Successfully sent message:", response); return event.data.adminRef.vel(); }) .catch(function(error) { console.log("Error sending message:", error); }); }); 的文档怎么样?或者甚至只是在你的python shell中测试它:

datetime.timedelta

如您所见,您已掌握了相关信息。

请注意,如果您需要基于小时(而非天数)的其他计算,则需要使用>>> d = datetime.timedelta(hours=24) >>> d datetime.timedelta(1) >>> dir(d) ['__abs__', '__add__', '__class__', '__delattr__', '__div__', '__doc__', '__eq__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__radd__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmul__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'days', 'max', 'microseconds', 'min', 'resolution', 'seconds', 'total_seconds'] >>> d.days 1 >>> d = datetime.timedelta(hours=8) >>> d.days 0 - 由于某些原因,我无法理解没有人负责添加{{1} }或delta.seconds / (60 * 60)属性为hours ...