赞
踩
#1--时间戳转化为日期 import datetime,time timestamp = 163642240800 # 转换成localtime time_local = time.localtime(timestamp / 1000) # 转换成新的时间格式(精确到秒) dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local) print(dt) #2021-11-09 09:46:48 d = datetime.datetime.fromtimestamp(timestamp / 1000) # 精确到毫秒 str1 = d.strftime("%Y-%m-%d %H:%M:%S.%f") print(str1) #2021-11-09 09:46:48.000000 #2-日期转化为时间戳 # 字符类型的时间 tss1 = '2021-06-03 21:19:03' # 转为时间数组 timeArray = time.strptime(tss1, "%Y-%m-%d %H:%M:%S") print(timeArray) # timeArray可以调用tm_year等 print(timeArray.tm_year) print(timeArray.tm_yday) # 转为时间戳 timeStamp = int(time.mktime(timeArray)) print(timeStamp) #3-python获取指定日期的前N天日期和后N天日期 #encoding:utf-8 from datetime import date, datetime, timedelta day = date.today() now = datetime.now() delta = timedelta(days=5) n_days_after = now + delta n_days_forward = now - delta print(("当前日期:{}").format(day)) print("向后推迟5天的日期:{}".format(n_days_after.strftime('%Y-%m-%d'))) print("向前推5天的日期:{}".format(n_days_forward.strftime('%Y-%m-%d'))) # 或者: td = datetime.date.today() #获取22天之后的日期 tm = (td + datetime.timedelta(days=22)).strftime('%Y-%m-%d')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。