新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:pythontimedelta函數(shù)是什么?
本文教程操作環(huán)境:windows7系統(tǒng)、python 3.9.1,DELL G3電腦。

1、概念
timedalte是datetime中的一個對象,該對象表示兩個時間的差值。
2、創(chuàng)造方法
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
3、參數(shù)
參數(shù)都是可選,默認值為0。
4、只讀屬性
timedelta.min:負數(shù)時間差,相當于timedelta(-999999999)。
timedelta.max:正數(shù)時間差,相當于timedelta(days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999)。
timedelta.resolution:兩個時間的最小差值相當于timedelta(microseconds=1)。
5、實例
from datetime import datetime, timedelta
current_datetime = datetime.now()
# future dates
one_year_future_date = current_datetime + timedelta(days=365)
print('Current Date:', current_datetime)
print('One year from now Date:', one_year_future_date)
# past dates
three_days_before_date = current_datetime - timedelta(days=3)
print('Three days before Date:', three_days_before_date)對于python中的時間獲取,我們最近學習的datetime模塊可以解決。在這個模塊中還有許多函數(shù)可以供我們使用,比如時間的差值,就可以選擇對應的timedalte函數(shù)。這個函數(shù)比較特殊,有三種只讀屬性。
以上就是python中timedelta函數(shù)的講解,大家在掌握基礎的內容后,就可以解決時間差的計算問題了。學會后趕快就上方代碼進行練習吧。
當前名稱:創(chuàng)新互聯(lián)Python教程:pythontimedelta函數(shù)是什么?
新聞來源:http://fisionsoft.com.cn/article/ccsiggs.html


咨詢
建站咨詢
