开发者

python count down to event (in days and hours)

Please can someone advise on this, 开发者_运维技巧I've tried various methods but don't seem to be able to get it to work.

I just need a countdown from

datetime.now()

to

datetime(2011,05,05)

in days hours


You can use

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

to get a datetime.timedelta object describing the remaining time. The number of remaining days is delta.days, the remaining hours delta.seconds/3600. or delta.seconds//3600.


You could try this -

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)


>>> 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

Does this help?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜