[英]Developing cop bot, but I am having trouble comparing two times to each other
这就是我所做的:
#!C:\Users\matej\PycharmProjects\SupremeBot\venv\Scripts\python.exe
import datetime
import webbrowser
#ADDING "SHOW WHEN THE BOT STARTS FUNCTION"
itstime = datetime.time(12, 0,)
now = datetime.datetime.today()
print("This is time when the program was started.:")
print(now)
print("This is when it is time to cop!!:")
print(itstime)
#BOT WEB OPEN FUNCTIONALITY"
if now < itstime:
pass
if now == itstime:
webbrowser.open("http://www.supremenewyork.com")
我现在有时间问题:
C:\Users\matej\PycharmProjects\SupremeBot\venv\Scripts\python.exe C:/Users/matej/PycharmProjects/SupremeBot/SupremeBot.py
This is time when the program was started.:
2020-04-03 21:11:03.697831
This is when it is time to cop!!:
12:00:00
Traceback (most recent call last):
File "C:/Users/matej/PycharmProjects/SupremeBot/SupremeBot.py", line 26, in <module>
if now < itstime:
TypeError: '<' not supported between instances of 'datetime.datetime' and 'datetime.time'
Process finished with exit code 1
如何解决这个问题? 任何想法如何使它更好,更快或更稳定? 我会感谢任何回应。
问题是您将“完整”日期戳与时间戳进行比较,这是没有意义的。 这样想:哪个更大:2020 年 3 月 1 日,星期二,下午 12:30,还是下午 12:40? 这完全没有意义。
这一行:
TypeError: '<' not supported between instances of 'datetime.datetime' and 'datetime.time'
告诉您您正在比较不同类型的对象。
但是,比较 12:30 PM 和 12:40 PM确实很有意义 - 所以基本上,只比较时间戳。 如果您只想比较日期的时间部分,请参阅下面的问答以仅获取日期戳的时间部分:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.