Python-TypeError:必须为整数(获取类型为datetime.datetime)

时间:2020-08-08 22:25:17

标签: python python-3.x

我有以下代码:

import datetime
from datetime import datetime as dt

def ceil_dt(dt, delta):
    return dt + (dt.min - dt) % delta

NextInterval5m = ceil_dt(now, timedelta(minutes=5))

unixtime5m = dt.fromtimestamp(NextInterval5m)

问题是我不断收到以下错误:

TypeError: an integer is required (got type datetime.datetime)

有人可以帮我吗?我不了解我应该如何转换NextInterval5m才能使其正常工作。我正在尝试将NextInterval5m转换为Unix时间戳

2 个答案:

答案 0 :(得分:2)

您应该能够通过在projectC对象上使用.timestamp()将其转换为unix时间戳。但是,此功能是Python 3所独有的。如果您需要python 2,则可以使用datetime.datetime,它需要一个.total_seconds()对象。

文档:https://docs.python.org/3.8/library/datetime.html#datetime.datetime.timestamp

答案 1 :(得分:1)

如果您使用的是python 3.3+,请使用>>> player = Assassin("Bob") >>> player.name 'Bob' >>> player.profession 'Assassin' >>> player.equip(Weapon("starter_dagger", 4, 0, 10, 1, "Assassin starter weapon")) >>> player.attack_power 39 >>> player.agility 50 >>> player.equip(Weapon("starter_longsword", 12, 0, -2, 2, "Unnecessarily heavy longsword")) >>> player.attack_power 47 >>> player.agility 38

Monster.startPathVector(fbb, 2);
Vec3.createVec3(builder, 1.0f, 2.0f, 3.0f);
Vec3.createVec3(builder, 4.0f, 5.0f, 6.0f);
int path = fbb.endVector();

输出:

.timestamp()

OR

import datetime
from datetime import datetime as dt
from datetime import timedelta

def ceil_dt(dt, delta):
    return dt + (dt.min - dt) % delta

now = dt.now()
NextInterval5m = ceil_dt(now, timedelta(minutes=5))
unixtime5m = NextInterval5m.timestamp()
print(unixtime5m)