1. 程式人生 > >Python 日期模組 --time

Python 日期模組 --time

– Start

from datetime import time


# 構造 time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
t = time(16, 28, 22)
t = time.fromisoformat('16:28:22')


# 類屬性
print(time.min)         # 00:00:00
print(time.max)         # 23:59:59.999999
print(time.resolution)  # 0:00:00.000001


# 例項屬性
print(t.hour)          # 16
print(t.minute)        # 28
print(t.second)        # 22
print(t.microsecond)   # 0
print(t.tzinfo)        # None
print(t.fold)          # 0


# 方法
print(t.isoformat())   # 16:28:22
print(t.strftime('%H:%M:%S'))   # 16:28:22

# time 是不可變的,replace 不會修改原 time
next_hour = t.replace(hour=t.hour + 1)
print(next_hour)

print(t.tzname())    # 時區名
print(t.utcoffset()) # 相對 UTC 時差
print(t.dst())       # 轉換為夏令時


# 比較時間
t1 = time(16, 28, 22)
t2 = time(16, 28, 23)
if t1 < t2:
    print(f'{t1} is before {t2}')

– 更多參見: – 聲 明:轉載請註明出處 – Last Updated on 2018-09-22 – Written by ShangBo on 2018-09-22 – End