Skip to content

Instantly share code, notes, and snippets.

@signed0
Created February 14, 2012 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save signed0/1829916 to your computer and use it in GitHub Desktop.
Save signed0/1829916 to your computer and use it in GitHub Desktop.
Human readable date range
def daterange(v0, v1):
assert v0 <= v1
if v0 == v1:
return v0.strftime('%b %d, %Y')
if v0.year == v1.year:
if v0.month == v1.month:
parts = (v0.strftime('%b %d'),
v1.strftime('%d'),
v0.year
)
return '%s-%s, %i' % parts
else:
parts = (v0.strftime('%b %d'),
v1.strftime('%b %d'),
v0.year
)
return '%s - %s, %i' % parts
else:
return '%s - %s' % (v0.strftime('%b %d, %Y'), v1.strftime('%b %d, %Y'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment