Reputation: 2312
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
Upvotes: 4
Views: 19550
Reputation: 601401
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
.
Upvotes: 12
Reputation: 12700
>>> 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?
Upvotes: 0
Reputation: 73588
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)
Upvotes: 0