if isinstance(time,datetime): now=datetime.now() timestamp=(now-time).total_seconds() if timestamp<60: return '刚刚' elif timestamp>=60 and timestamp<=60*60: minutes=timestamp /60 return '%s 分钟前'% int(minutes) elif timestamp>=60*60 and timestamp<60*60*24: hours=timestamp/(60*60) return '%s小时前'% int(hours) elif timestamp>=60*60*24 and timestamp<60*60*24*30: days=timestamp/(60*60*24) return '%s天前'% int(days) else: return time.strftime('%Y/%m/%d/ %H:%M'
html:
<p>发表时间:{{ create_time|handle_time }}</p>