问题是我希望它忽略日期,只考虑时间因素.这是我有的:
import time
from time import mktime
from datetime import datetime
def getTimeCat(Datetime):
# extract time categories
str_time = datetime.strptime(Datetime, "%m/%j/%y %H:%M")
ts = datetime.fromtimestamp(mktime(str_time))
# --> Morning = 0400-1000
mornStart = datetime.time(4, 0, 1)
mornEnd = datetime.time(10, 0, 0)
# --> Midday = 1000-1600
midStart = datetime.time(10, 0, 1)
midEnd = datetime.time(16, 0, 0)
# --> Evening = 1600-2200
eveStart = datetime.time(16, 0, 1)
eveEnd = datetime.time(22, 0, 0)
# --> Late Night = 2200-0400
lateStart = datetime.time(22, 0, …Run Code Online (Sandbox Code Playgroud) 我试图转换两个"持续时间",但我目前收到的是TypeError由于一个是一个datetime.timedelta和一个是datetime.time:
TypeError: unorderable types: datetime.time() <= datetime.timedelta()
Run Code Online (Sandbox Code Playgroud)
将a转换datetime.time为a 的有效方法是datetime.timedelta什么?
我已经检查了文档,并且没有内置的方法来在这两种类型之间进行转换.