时间相关
1、用localtime,设置时区
os.environ['TZ'] = "Asia/Shanghai" time.tzset()
2、时间元组转化为时间格式
def dateformat_trans(tformat, date): newdate = time.strftime(tformat, date) return newdate
data_tuple = (2008, 2, 13, 0, 0, 0, 0, 0, 0) print (dateformat_trans("%Y%m%d", data_tuple))
3、时间整形转化为时间格式
dtstr = "20130415172559"
dt = datetime.datetime.strptime(dtstr, "%Y%m%d%H%M%S")
print(dt)
2013-04-15 17:25:59
import datetime
def dateformat_trans(date_str):
dtstr = date_str + "000000"
dt = datetime.datetime.strptime(dtstr, "%Y%m%d%H%M%S")
date_str = str(dt)[:10]
return date_str
print(dateformat_trans("20190313"))