一篇很简单的博文,很简单的内容,不过最近记性不好,备忘一下。
import datetime import time#定义string字段 str = '2019-05-11 00:00:00'#定义datetime字段dt = datetime.datetime.now()#定义timestamp字段ts = time.time()#把string转成datetimedef str2dt(str): return datetime.datetime.strptime(str, "%Y-%m-%d %H:%M:%S")#把string转成timestampdef str2ts(str): return time.mktime(time.strptime(str, "%Y-%m-%d %H:%M:%S"))#把timestamp转成stringdef ts2str(ts): return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))#把timestamp转成datetimedef ts2dt(ts): return datetime.datetime.fromtimestamp(ts)#把datetime转成stringdef dt2str(dt): return dt.strftime("%Y-%m-%d %H:%M:%S")#把datetime转成timestampdef dt2ts(dt): return time.mktime(dt.timetuple())