timedalte 是datetime中的一个对象,该对象表示两个时间的差值
构造函数:datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
其中参数都是可选,默认值为0
其中:
1 millisecond = 1000 microseconds
1 minute = 60 seconds
1 hour = 3600 seconds
1 week = 7 days
import datetime print('当前时间%s'%datetime.datetime.now()) print('默认时间是%s'%datetime.timedelta()) print('当前时间加3天%s'%(datetime.datetime.now()+datetime.timedelta(days=3))) print('当前时间减3天%s'%(datetime.datetime.now()+datetime.timedelta(days=-3))) print('当前时间加2周(14天)%s'%(datetime.datetime.now()+datetime.timedelta(weeks=2))) print('当前时间加5s%s'%(datetime.datetime.now()+datetime.timedelta(seconds=5))) print('当前时间加5分钟%s'%(datetime.datetime.now()+datetime.timedelta(minutes=5))) print('当前时间加5小时%s'%(datetime.datetime.now()+datetime.timedelta(hours=5))) D:studypython estvenvScriptspython.exe D:/study/python/test/dd.py 当前时间2019-06-14 11:58:40.512885 默认时间是0:00:00 当前时间加3天2019-06-17 11:58:40.512885 当前时间减3天2019-06-11 11:58:40.513884 当前时间加2周(14天)2019-06-28 11:58:40.513884 当前时间加5s2019-06-14 11:58:45.513884 当前时间加5分钟2019-06-14 12:03:40.513884 当前时间加5小时2019-06-14 16:58:40.513884