如何检查时间值?

Hou*_*man 1 python django time

我想知道给定的时间是在上午还是下午.我不确定这是否是在python中创建Time对象并进行比较的正确方法.或者,还有更好的方法?

def part_of_day_statistics(x):
    if x > time.strptime('6:00 am') and x < time.strptime('11:59 am'):
        return 'Morning' 
    if x > time.strptime('12:00 pm') and x < time.strptime('5:59 pm'):
        return 'Afternoon' 
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 6

Django TimeField条目datetime.time实例,它具有一个.hour属性(范围从0到23):

def part_of_day_statistics(x):
    if x.hour >= 6 and x.hour < 12:
        return 'Morning'
    if x.hour >= 12 and x.hour < 18:
        return 'Afternoon'
Run Code Online (Sandbox Code Playgroud)