Neil Cerutti <ne...@norwich.edu> writes: > On 2010-07-23, Jim <jim.heffe...@gmail.com> wrote: >> How can I calculate how much time is between now and the next >> 2:30 am? Naturally I want the system to worry about leap >> years, etc. > > You need the datetime module. Specifically, a datetime and > timedelta object.
Although it sounds like the question is to derive the timedelta value, so it's not known up front. That's a little trickier since you'd need to construct the datetime object for "next 2:30 am" to subtract "now" from to get the delta. But that requires knowing when the next day is, thus dealing with month endings. Could probably use the built-in calendar module to help with that though. For the OP, you might also take a peek at the dateutil third party module, and its relativedelta support, which can simplify the creation of the "next 2:30 am" datetime object. Your case could be handled by something like: from datetime import datetime from dateutil.relativedelta import relativedelta target = datetime.now() + relativedelta(days=+1, hour=2, minute=30, second=0, microsecond=0) remaining = target - datetime.now() This ends up with target being a datetime instance for the next day at 2:30am, and remaining being a timedelta object representing the time remaining, at least as of the moment of its calculation. Note that relativedelta leaves fields alone that aren't specified, so since datetime.now() includes down to microseconds, I clear those explicitly). Since you really only need the date, you could also use datetime.date.today() instead as the basis of the calculation and then not need second/microsecond parameters to relativedelta. -- David -- http://mail.python.org/mailman/listinfo/python-list