本文例子用
from datetime import *
关于python的import和名字空间,请参考之前文章:
《Python日期时间datetime库与dateutil库典型示例与详解(1)--datetime库简介&datetime库date类常用例子》
timedelta常用的是用来计算两个日期(日期时间)对象的间隔:
通常生成timedelta对象有两种方法:
(1)各种时间单位单个或者组合的方式生成timedelta对象
mytimedelta = timedelta (
days=50,
seconds=27,
microseconds=10,
milliseconds=29000,
minutes=5,
hours=8,
weeks=2)
(2)datetime对象之间、date对象之间做减法运算
timedelta = date1 - date2
timedelta = datetime1 - datetime2
timedelta对象仅保存有以下三个字段值供我们获取使用(都是int类型):
days= mytimedelta.days
seconds = mytimedelta.seconds
microseconds = mytimedelta. microseconds
(1)days(有效值范围-999999999 到999999999)
(2)seconds(有效值范围0 到86399,刚好是1天的秒数)
(3)microseconds(有效值范围0 到999999)
换句话说,timedelta是微秒、秒、天为单位相互进位的,其他几个我们milliseconds/ minutes/ hours/weeks时间单位的初始值在生成timedelta对象时会自动转换为days/ seconds/ microseconds三个时间单位。
我们上面生成的mytimedelta对象等价于
mytimedelta = timedelta(days=64, seconds=29156, microseconds=10)
date1 = date(2023,11,16)
date2 = date(2023,11,19)
date_timedelta = date1 - date2
#两个date对象只能做减法运算,其他运算都抛异常,date_timedelta的数据结构是timedelta类对象,显然我们得到的date_timedelta其seconds、microseconds都是0,只有days
days= date_timedelta.days
datetime1 = datetime(2023,11,17,12,12,12)
datetime2 = datetime(2023,11,18,13,13,13)
datetime_timedelta = datetime2 - datetime1
#两个datetime对象只能做减法运算,其他运算都抛异常
days= datetime_timedelta.days #days值为1
seconds = datetime_timedelta.seconds # seconds为3661
我们计算两个datetime对象的间隔,不能直接得到他们差了多少小时、分、秒,只能得到days/ seconds/ microseconds要自己根据seconds自己算一下;
有以下两个对象:
date1 = date(2023,11,16)
datetime1 = datetime(2023,11,17,12,12,12)
date和datetime对象之间不能直接做时间间隔运算,会抛异常,因此计算前我们要先掌握date和datetime对象的相互转换。
详见系列文章:《Python日期时间datetime库与dateutil库典型示例与详解(2)--datetime库datetime类常用例子》
中的“datetime与date类型的相互转换”小节
(1)计算date和datetime对象间隔天数
timedelta = datetime1.date() - date1
days = timedelta.days
(2)计算date和datetime对象间隔详细时间
timedelta = datetime1 - datetime.strptime(str(date1),'%Y-%m-%d')
days = timedelta.days
seconds = timedelta.seconds
有以下两个对象:
date1 = date(2023,11,16)
date2 = date(2023,11,17)
datetime1 = datetime(2023,11,16,12,12,12)
date和datetime对象之间不能直接做逻辑运算,会抛异常,因此计算前我们要先掌握date和datetime对象的相互转换。
详见系列文章:《Python日期时间datetime库与dateutil库典型示例与详解(2)--datetime库datetime类常用例子》
中的“datetime与date类型的相互转换”小节
if datetime.combine(date1, time()) > datetime1:
#结果为False
if datetime.combine(date2, time()) > datetime1:
#结果为True
假设一下对象:
date1 = date(2023,11,16)
datetime1 = datetime(2023,11,17,12,12,12)
timedelta1= timedelta (days=3)
timedelta2= timedelta (days=3, seconds=90)
(1)date对象与timedelta对象的运算
date2 = date1 + timedelta1
date3 = date1 + timedelta2
计算结果date2和date3是完全一样的,date与timedelta计算,会直接忽略timedelta对象的seconds/ microseconds两个单位的值,只用days字段值进行计算,
(2)datetime对象与timedelta对象的运算
timedelta对象的days/ seconds/ microseconds三个字段都会参与到运算,自动转换成datetime对象的时分秒单位;
datetime2 = datetime1 + timedelta2
#datetime2结果为(2023,11,20,12,13,42)
(3)date/datetime对象跟timedelta对象只能做减法运算,其他运算都抛异常
timedelta对象之间可以做很多运算
Operation |
Result |
t1 = t2 + t3 |
Sum of t2 and t3. Afterwards t1-t2 == t3 and t1-t3 == t2 are true. (1) |
t1 = t2 - t3 |
Difference of t2 and t3. Afterwards t1 == t2 - t3 and t2 == t1 + t3 are true. (1)(6) |
t1 = t2 * i t1 = i * t2 |
Delta multiplied by an integer. Afterwards t1 // i == t2 is true, provided i != 0. |
In general, t1 * i == t1 * (i-1) + t1 is true. (1) |
|
t1 = t2 * f t1 = f * t2 |
Delta multiplied by a float. The result is rounded to the nearest multiple of timedelta.resolution using round-half-to-even. |
f = t2 / t3 |
Division (3) of overall duration t2 by interval unit t3. Returns a float object. |
t1 = t2 / f t1 = t2 / i |
Delta divided by a float or an int. The result is rounded to the nearest multiple of timedelta.resolution using round-half-to-even. |
t1 = t2 // i t1 = t2 // t3 |
The floor is computed and the remainder (if any) is thrown away. In the second case, an integer is returned. (3) |
t1 = t2 % t3 |
The remainder is computed as a timedelta object. (3) |
q, r = divmod(t1, t2) |
Computes the quotient and the remainder: q = t1 // t2 (3) and r = t1 % t2. q is an integer and r is a timedelta object. |
+t1 |
Returns a timedelta object with the same value. (2) |
-t1 |
equivalent to timedelta(-t1.days, -t1.seconds, -t1.microseconds), and to t1* -1. (1)(4) |
abs(t) |
equivalent to +t when t.days >= 0, and to -t when t.days < 0. (2) |
str(t) |
Returns a string in the form [D day[s], ][H]H:MM:SS[.UUUUUU], where D is negative for negative t. (5) |
repr(t) |
Returns a string representation of the timedelta object as a constructor call with canonical attribute values. |
Notes:
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00