Created
September 21, 2012 10:34
-
-
Save jamiesun/3760829 to your computer and use it in GitHub Desktop.
convtime
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
import datetime | |
def convtime(ctime): | |
if not ctime: | |
return '' | |
cdate = datetime.datetime.strptime(ctime,'%Y-%m-%d %H:%M:%S') | |
nowdate = datetime.datetime.now() | |
dt = nowdate - cdate | |
secs = dt.total_seconds() | |
if secs < 60: | |
return u"刚刚" | |
minute = int(secs/60) | |
if minute >= 1 and minute < 60 : | |
return u"%s分钟前"%minute | |
hours = int(secs / (60*60)) | |
if hours >= 1 and hours < 24 : | |
return u"%s小时前"%hours | |
days = int(secs / (60*60*24)) | |
if days >=1 and days<31: | |
return u"%s天前"%days | |
months = int(secs / (60*60*24*30)) | |
if months >=1 and months<12: | |
return u"%s月前"%months | |
years = int(secs / (60*60*24*365)) | |
return u"%s年前"%years |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment