Skip to content

Instantly share code, notes, and snippets.

@zhyq0826
Last active September 4, 2016 08:41
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 zhyq0826/0444cf577fe4595ba062 to your computer and use it in GitHub Desktop.
Save zhyq0826/0444cf577fe4595ba062 to your computer and use it in GitHub Desktop.
时间戳显示
from datetime import datetime
def format_show_time(v, start=None, end=None):
diff = datetime.now() - v
if diff.seconds < 60:
s = ('%s%s') % (diff.seconds, '秒前')
elif diff.seconds >= 60 and diff.seconds < 3600:
s = ('%s%s') % (diff.seconds / 60, '分钟前')
elif diff.seconds >= 3600 and (v > start and v < end):
s = ('%s%s') % ('今天', datetime.strftime(v, format='%H:%M'))
else:
s = datetime.strftime(v, format='%Y-%m-%d %H:%M')
return s
if __name__ == '__main__':
format_show_time(datetime(2016,9,6,12,13), datetime(2016,9,0,0,0), datetime(2016,9,15,0,0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment