Created
November 17, 2010 17:32
-
-
Save cfmonkey/703700 to your computer and use it in GitHub Desktop.
Using timedelta to calculate two datatime objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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