# 5.1.2 timedelta对象

timedelta对象是用来表示时间的区间，主要是两个日期或时间之差。

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

l 一毫秒等于1000微秒

l 一分钟等于60

l 一小时等于3600

l 一周等于7

l 0  <= 微秒 < 1000000

l 0  <= 秒 < 3600*24

l -999999999  <= 天数 <= 999999999

#python 3.4.3

import datetime

d = datetime.timedelta(microseconds=-1)

print(d.days, d.seconds, d.microseconds)

-1 86399 999999

timedelta类拥有的属性如下：

timedelta.min

#python 3.4.3

import datetime

d = datetime.timedelta(microseconds = 0)

print(datetime.timedelta.min)

-999999999 days, 0:00:00

timedelta.max

#python 3.4.3

import datetime

d = datetime.timedelta(microseconds = 0)

print(datetime.timedelta.min)

print(datetime.timedelta.max)

-999999999 days, 0:00:00

999999999 days, 23:59:59.999999

timedelta.resolution

#python 3.4.3

import datetime

d = datetime.timedelta(microseconds = 0)

print(datetime.timedelta.min)

print(datetime.timedelta.max)

print(datetime.timedelta.resolution)

-999999999 days, 0:00:00

999999999 days, 23:59:59.999999

0:00:00.000001

timedelta实例只读属性：

 属性 值 days 在范围-999999999 和 999999999 之间。 seconds 在范围0和86399之间。 microseconds 在范围0和999999之间。

#python 3.4.3

import datetime

d = datetime.timedelta(microseconds = 12348850005600)

print(d.days)

print(d.seconds)

print(d.microseconds)

142

80050

5600

timedelta类支持的操作：

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 0)

t3 = datetime.timedelta(microseconds = 8)

t1 = t2 + t3

print('t1 - t2 = :', t1 - t2 == t3)

print(t1.days)

print(t1.seconds)

print(t1.microseconds)

t1 - t2 = : True

0

0

8

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 0)

t3 = datetime.timedelta(microseconds = 8)

t1 = t2 - t3

print('t1 - t2 = :', t1 + t3 == t2)

print(t1.days)

print(t1.seconds)

print(t1.microseconds)

t1 - t2 = : True

-1

86399

999992

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

t1 = t2*9

t4 = t3*9

print(t1.days)

print(t1.seconds)

print(t1.microseconds)

print(t4.days)

print(t4.seconds)

print(t4.microseconds)

0

0

81

9

0

72

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

t1 = t2*9.5

t4 = t3*9.5

print(t1.days)

print(t1.seconds)

print(t1.microseconds)

print(t4.days)

print(t4.seconds)

print(t4.microseconds)

0

0

86

9

43200

76

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

t1 = t2 / t3

print(t1)

1.041666666570216e-10

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

t1 = t2 / 0.5

t4 = t3 / 9

print(t1)

print(t1.microseconds)

print(t4)

print(t4.microseconds)

0:00:00.000018

18

2:40:00.000001

1

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

t1 = t2 // 5

t4 = t3 // t2

print(t1)

print(t1.microseconds)

print(t4)

0:00:00.000001

1

9600000000

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

t1 = t2 % t3

print(t1)

print(t1.microseconds)

0:00:00.000009

9

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

q, r = divmod(t2, t3)

print(1)

print(r.microseconds)

1

9

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

t1 = +t2

t4 = +t3

print(t2, t3)

print(t1, t4)

0:00:00.000009 1 day, 0:00:00.000008

0:00:00.000009 1 day, 0:00:00.000008

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

t1 = -t2

t4 = -t3

print(t2, t3)

print(t1, t4)

0:00:00.000009 1 day, 0:00:00.000008

-1 day, 23:59:59.999991 -2 days, 23:59:59.999992

timedelta对象取绝对值abs(t)

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

t1 = abs(-t2)

t4 = -t3

print(t2, t3)

print(t1, t4)

0:00:00.000009 1 day, 0:00:00.000008

0:00:00.000009 -2 days, 23:59:59.999992

timedelta对象按格式[D day[s], ][H]H:MM:SS[.UUUUUU]来格式化。当t是负值的，那么D也是负值的。

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

print(str(t2), str(-t3))

0:00:00.000009 -2 days, 23:59:59.999992

repr(t)运算

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

print(repr(t2), repr(-t3))

datetime.timedelta(0, 0, 9) datetime.timedelta(-2, 86399, 999992)

timedelta.total_seconds()

#python 3.4.3

import datetime

t2 = datetime.timedelta(microseconds = 9)

t3 = datetime.timedelta(days = 1, microseconds = 8)

print(repr(t2), repr(-t3))

print(t2.total_seconds(), t3.total_seconds())

datetime.timedelta(0, 0, 9) datetime.timedelta(-2, 86399, 999992)

9e-06 86400.000008

caimouse

#### Python datetime 模块之timedelta

2017-01-04 18:27:37

#### Python中时间的处理之——timedelta篇

2015-04-07 14:49:28

#### datetime.timedelta类介绍

2018-01-20 11:38:29

#### Pandas详解六之Timestamp、Period、Timedelta时间对象

2018-03-17 22:28:41

#### python+pandas+时间、日期以及时间序列处理

2017-06-27 19:36:54

#### time模块与datetime以及timedelta模块基础用法

2017-11-06 16:34:20

#### python的timedelta

2013-05-08 16:25:25

#### Python中datetime.timedelta的应用

2013-03-27 21:36:58

#### 对Time.deltaTime的简单理解

2016-10-14 11:30:40