[英]Python - TypeError: an integer is required (got type datetime.datetime)
I have the following code:我有以下代码:
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)
The problem is that i keep getting the following error:问题是我不断收到以下错误:
TypeError: an integer is required (got type datetime.datetime)
Can someone help me out on this?有人可以帮我解决这个问题吗? I don't understand to what i am supposed to convert NextInterval5m
in order to make it work.我不明白我应该转换NextInterval5m
以使其工作。 I'm trying to convert NextInterval5m
to an Unix timestamp我正在尝试将NextInterval5m
转换为 Unix 时间戳
You should be able to convert it into a unix timestamp by using .timestamp()
on a datetime.datetime
object.您应该能够通过在datetime.datetime
object 上使用.timestamp()
将其转换为 unix 时间戳。 However, this function is exclusive to Python 3. If you need something for python 2, you can use .total_seconds()
which requires a datetime.time_delta
object instead. However, this function is exclusive to Python 3. If you need something for python 2, you can use .total_seconds()
which requires a datetime.time_delta
object instead.
Documentation: https://docs.python.org/3.8/library/datetime.html#datetime.datetime.timestamp文档: https://docs.python.org/3.8/library/datetime.html#datetime.datetime.timestamp
If you are using python 3.3+, use .timestamp()
如果您使用的是 python 3.3+,请使用.timestamp()
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)
Output: Output:
1596926400.0
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((NextInterval5m - datetime.datetime(1970,1,1)).total_seconds())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.