time
def parse_time(self, date): if re.match('刚刚', date): date = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time())) if re.match('\d+分钟前', date): minute = re.match('(\d+)', date).group(1) date = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - float(minute) * 60)) if re.match('\d+小时前', date): hour = re.match('(\d+)', date).group(1) date = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - float(hour) * 60 * 60)) if re.match('昨天.*', date): date = re.match('昨天(.*)', date).group(1).strip() date = time.strftime('%Y-%m-%d', time.localtime() - 24 * 60 * 60) + ' ' + date if re.match('\d{2}-\d{2}', date): date = time.strftime('%Y-', time.localtime()) + date + ' 00:00' return date
import time; # 引入time模块 # 当前时间戳 ticks = time.time() print "当前时间戳为:", ticks # 1459994552.51 # 获取当前时间 localtime = time.localtime(time.time()) print "本地时间为 :", localtime
import time # 格式化成2016-03-20 11:45:39形式 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 格式化成Sat Mar 28 22:24:24 2016形式 print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) # 将格式字符串转换为时间戳 a = "Sat Mar 28 22:24:24 2016" print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")) # 2016-04-07 10:25:09 # 常用 # Thu Apr 07 10:25:09 2016 # 1459175064.0
import datetime import json dic = { 'k1':123, 'ctime':datetime.datetime.now() } class MyEncoder(json.JSONEncoder): def default(self, o): # o是数据类型 if isinstance(o,datetime.datetime): return o.strftime('%Y-%m-%d') else: return super(MyEncoder,self).default(o) v = json.dumps(dic,cls=MyEncoder) print(v) # {"k1": 123, "ctime": "2018-05-15"}