Last active
September 4, 2016 08:41
-
-
Save zhyq0826/0444cf577fe4595ba062 to your computer and use it in GitHub Desktop.
时间戳显示
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 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