Skip to content

Instantly share code, notes, and snippets.

@squarepegsys
Created October 9, 2013 18:00
Show Gist options
  • Save squarepegsys/6905476 to your computer and use it in GitHub Desktop.
Save squarepegsys/6905476 to your computer and use it in GitHub Desktop.
Trying to get "X days, Y hours, and Z minutes" string with a timedelta. Also, try to use singular when the value is 1
def get_label(label,val):
if val!=1:
return label+"s"
return label
diff = somedate -datetime.now()
hours, remainder = divmod(diff.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
countdown = "{0.days} {1}, {2} {3}, & {4} {5}".format(
diff, get_label("day",diff.days),
hours, get_label("hour",hours),
minutes, get_label("minute",minutes)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment