pythontimedelta函數(shù)是什么?
本文教程操作環(huán)境:windows7系統(tǒng)、Python3.9.1,DELLG3電腦。
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、實例
fromdatetimeimportdatetime,timedelta
current_datetime=datetime.now()
#futuredates
one_year_future_date=current_datetime+timedelta(days=365)
print('CurrentDate:',current_datetime)
print('OneyearfromnowDate:',one_year_future_date)
#pastdates
three_days_before_date=current_datetime-timedelta(days=3)
print('ThreedaysbeforeDate:',three_days_before_date)
對于python中的時間獲取,我們最近學習的datetime模塊可以解決。在這個模塊中還有許多函數(shù)可以供我們使用,比如時間的差值,就可以選擇對應的timedalte函數(shù)。這個函數(shù)比較特殊,有三種只讀屬性。
以上就是python中timedelta函數(shù)的講解,大家在掌握基礎(chǔ)的內(nèi)容后,就可以解決時間差的計算問題了。學會后趕快就上方代碼進行練習吧。更多Python學習教程請關(guān)注IT培訓機構(gòu):千鋒教育。