-
-
Save dannyvoid/06c03a1f022534a81eb55de678ccf54e to your computer and use it in GitHub Desktop.
Python countdown to date script
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
from datetime import datetime | |
def time_to_seconds(date1, date2): | |
timedelta = date2 - date1 | |
return timedelta.days * 24 * 3600 + timedelta.seconds | |
def human_time(seconds): | |
minutes, seconds = divmod(seconds, 60) | |
hours, minutes = divmod(minutes, 60) | |
days, hours = divmod(hours, 24) | |
return (days, hours, minutes, seconds) | |
now = datetime.now() | |
event = datetime.strptime("2021-12-25 00:00:00", "%Y-%m-%d %H:%M:%S") | |
countdown = "{} days, {} hours, {} minutes, and {} seconds".format( | |
*human_time(time_to_seconds(now, event)) | |
) | |
print(countdown) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment