Skip to content

Instantly share code, notes, and snippets.

@cfmonkey
Created November 17, 2010 17:32
Show Gist options
  • Select an option

  • Save cfmonkey/703700 to your computer and use it in GitHub Desktop.

Select an option

Save cfmonkey/703700 to your computer and use it in GitHub Desktop.
Using timedelta to calculate two datatime objects
def diff_time(curr_time):
curr_time_obj = datetime.strptime(curr_time, "%a, %d %b %Y %H:%M:%S GMT")
now_obj = datetime.now()
res = curr_time_obj - now_obj
days = int(abs(res.total_seconds()) / (3600 * 24))
hours = int((abs(res.total_seconds()) - days * 3600 * 24)/3600)
minutes = int((abs(res.total_seconds()) - days * 3600 * 24 - hours * 3600)/60)
ret = u"From now "
if days > 0:
ret += str(days) + "days "
if hours > 0:
ret += str(hours) + "hours "
if minutes > 0:
ret += str(minutes) + "minutes "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment