【Python】datetime時間計算

使用datetime庫

import datetime

1.獲取當前時間

current_time = datetime.datetime.now()

2.當前時間後的十秒

datetime.datetime.now() + datetime.timedelta(seconds=10)

3.當前時間後一天

datetime.datetime.now() + datetime.timedelta(days=1)

此外還有minutes,hours參數,與前面同理

4.時間的相減與比較
可以直接相減,也可直接用運算符比較

如:

current_time = datetime.datetime.now()
stop_time = datetime.timedelta(seconds=10)

# compare time
if current_time < stop_time:
	print(True)

# calculate difference of two time
diff = stop_time - current_time
print(diff)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章