每天CookBook之Python-052

  • 时间转换
from datetime import timedelta

a = timedelta(days=2, hours=6)
b = timedelta(hours=4.5)
c = a + b

print(c.days)
print(c.seconds)
print(c.seconds / 3600)
print(c.total_seconds() / 3600)

from datetime import datetime
a = datetime(2012, 9, 23)

print(a + timedelta(days=10))

b = datetime(2012, 12, 21)
d = b - a

print(d.days)

now = datetime.today()
print(now)
print(now + timedelta(minutes=10))

a = datetime(2012, 3, 1)
b = datetime(2012, 2, 28)

print(a - b)
print((a - b).days)

c = datetime(2013, 3, 1)
d = datetime(2013, 2, 28)
print((c - d).days)

Out

2
37800
10.5
58.5
2012-10-03 00:00:00
89
2016-07-20 23:13:28.128843
2016-07-20 23:23:28.128843
2 days, 0:00:00
2
1

Process finished with exit code 0

posted @ 2016-07-20 23:27  4Thing  阅读(98)  评论(0编辑  收藏  举报