python timedelta函数是什么?

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

1、概念

timedalte是datetime中的一个对象,该对象表示两个时间的差值

2、创造方法

datetime.timedelta(days=0,seconds=0,microseconds=0,milliseconds=0,minutes=0,hours=0,weeks=0)

3、参数

参数都是可选,默认值为0。

4、只读属性

timedelta.min:负数时间差,相当于timedelta(-999999999)。

timedelta.max:正数时间差,相当于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模块可以解决。在这个模块中还有许多函数可以供我们使用,比如时间的差值,就可以选择对应的timedalte函数。这个函数比较特殊,有三种只读属性。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。