python倒计时到事件(在几天和几小时)

时间:2010-12-01 17:15:38

标签: python datetime

请有人就此提出建议,我尝试了各种方法,但似乎无法让它发挥作用。

我只需要从

倒计时
datetime.now()

datetime(2011,05,05)

以天为单位

3 个答案:

答案 0 :(得分:9)

您可以使用

delta = datetime.datetime(2011, 5, 5) - datetime.datetime.now()

获取描述剩余时间的datetime.timedelta对象。剩余天数为delta.days,剩余时间为delta.seconds/3600.delta.seconds//3600

答案 1 :(得分:0)

你可以试试这个 -

import datetime

dt  = datetime.datetime
now = dt.now()

# This gives timedelta in days
dt(year=2011,month=05,day=05) - dt(year=now.year, month=now.month, day=now.day)

# This gives timedelta in days & seconds
dt(year=2011,month=05,day=05) - dt(year=now.year, month=now.month, day=now.day, minute=now.minute)

答案 2 :(得分:0)

>>> days_till_doomsday = \
...   (datetime.datetime(2011,05,05) - datetime.datetime.now()).days
>>> days_till_doomsday
154
>>> hours_till_midnight_today = 24 - datetime.datetime.now().hour
>>> hours_till_midnight_today
5
>>> hours_till_doomsday = \
...   (days_till_doomsday * 24) + hours_till_midnight_today
>>> hours_till_doomsday
3701

这有帮助吗?