这就是我所做的:
#!C:UsersmatejPycharmProjectsSupremeBotvenvScriptspython.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:UsersmatejPycharmProjectsSupremeBotvenvScriptspython.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与12:40进行比较是完全合理的,因此基本上只比较时间戳。如果你只想比较日期的时间部分,请参阅问答;下面只获取日期戳的时间部分:
Python:从`datetime.datetime`转换为`time.time`