Python datetime 模块

Python time() 函数用于表示时间(不含日期)。

语法

time(hour=0, minute=0, second=0, microsecond=0)

参数

hourminutesecondmicrosecond,默认 0。

返回值

返回 time 时间对象(不含日期)。

示例

from datetime import time

t = time(14, 30, 15)
print(t)                      # 14:30:15
print(t.hour, t.minute, t.second)   # 14 30 15

t2 = time(8)
print(t2)                     # 08:00:00

print(t.strftime("%H:%M"))    # 14:30

注意事项

time 对象表示一天内的时刻。判断时间段用 t.hour 比较。