Created
February 14, 2012 20:28
-
-
Save signed0/1830094 to your computer and use it in GitHub Desktop.
Human readable time fields
This file contains 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 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