python timedelta函数是什么?
pythontimedelta函数是什么?
本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。
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函数。这个函数比较特殊,有三种只读属性。
以上就是python中timedelta函数的讲解,大家在掌握基础的内容后,就可以解决时间差的计算问题了。学会后赶快就上方代码进行练习吧。更多Python学习教程请关注IT培训机构:开发教育。
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!