Skip to content

Instantly share code, notes, and snippets.

@signed0
Created February 14, 2012 20:28
Show Gist options
  • Save signed0/1830094 to your computer and use it in GitHub Desktop.
Save signed0/1830094 to your computer and use it in GitHub Desktop.
Human readable time fields
from datetime import datetime
def datetime_format(value):
'''Provides pretty datetime strings
The amount of information will change depending on how long ago the value is from now
If the value is today then just the time will be returned
If the value is this year then the year will be left off
'''
now = datetime.utcnow()
assert value < now, "Not for future times"
if now.date == value.date:
return value.strftime('%I:%M %p')
if value.year == now.year:
return value.strftime('%B %d')
else:
return value.strftime('%B %d %Y')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment