Skip to content

Instantly share code, notes, and snippets.

@jamiesun
Created September 21, 2012 10:34
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 jamiesun/3760829 to your computer and use it in GitHub Desktop.
Save jamiesun/3760829 to your computer and use it in GitHub Desktop.
convtime
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