利用datetime模組進行不同時間單位的換算
Python代碼
from datetime import timedelta a = timedelta(days=2, hours=6) b = timedelta(hours=4.5) c = a + b print(c.days) print(c.seconds) print(c.seconds / 3600)
表示特定的日期和時間
Javafrom datetime import datetime a = datetime(2016, 8, 30) print(a + timedelta(days=10)) b = datetime(2016, 9, 30) d = b - a print(d.days) now = datetime.today() print(now) print(now + timedelta(minutes=10))
處理更為複雜的日期問題,如處理時區,模糊時間範圍,計算節日的日期等可以用dateutil模組