时间转化总结
获取时间戳
import time
a=time.time() 带小数的时间戳
b=int(a)
print(a)
时间戳转化为日期
python下将时间戳转换到日期
import datetime
import time
ltime=time.localtime(1395025933)
timeStr=time.strftime("%Y-%m-%d %H:%M:%S", ltime)
print timeStr
例如:
date=1563023653203254 时间戳
ltime = time.localtime(date)
end_date = time.strftime("%Y-%m-%d ", ltime)
now_week = int(time.strftime(("%d ")) # 获取当前天
now_week = int(time.strftime(("%w")) # 获取当前是周几
now_week = int(time.strftime(("%m")) # 获取当前第几月
获取当前日期时间
import datetime
a=datetime.datetime.now() 具体时间
print(a)
2019-04-29 10:51:36.45995
# 获取当前时间并转化为时间对象
time_now=datetime.datetime.now().strftime('%Y-%m-%d')
time_now=datetime.date(int(time_now[:4]), int(time_now[5:7]), int(time_now[-2:]))
Python下日期到时间戳的转换
import datetime
import time
dateC=datetime.datetime(2010,6,6,8,14,59)
timestamp=time.mktime(dateC.timetuple())
print timestamp
时间对象减价指定月
b="2016-08-31"
d="2019-02-20"
time_b=datetime.date(int(b[:4]),int(b[5:7]),int(b[-2:]))
time_d=datetime.date(int(d[:4]),int(d[5:7]),int(d[-2:]))
def add_months(dt,
months):
targetmonth = months + dt.month
try:
dt = dt.replace(year=dt.year + int(targetmonth / 12), month=(targetmonth % 12))
except:
if targetmonth+1==12:
dt=dt.replace(year=dt.year+int((targetmonth+1)/12),month=(int(12)),day=1)
else:
dt=dt.replace(year=dt.year+int((targetmonth+1)/12),month=((targetmonth+1)%12),
day=1)
dt-=datetime.timedelta(days=1)
return dt
time_c=add_months1(time_b,1)
print(time_c)
时间戳转换为时间格式
.FROM_UNIXTIME()函数
FROM_UNIXTIME(unix_timestamp,format)
参数unix_timestamp 时间戳可以用数据库里的存储时间数据的字段
参数format 要转化的格式 比如“”%Y-%m-%d“” 这样格式化之后的时间就是 2017-11-30
FROM_UNIXTIME(create_time, "%Y-%m-%d") AS dat
时间格式转化为时间戳
UNIX_TIMESTAMP(date)
其中date可以是一个DATE字符串,一个DATETIME字符串,一个TIMESTAMP或者一个当地时间的YYMMDD或YYYMMDD格式的数字
用这个函数可以帮助我们在时间戳中筛选出某些天的数据
def edit_timestamp(timestamp, is_timing, _type=0):
"""
:param timestamp: 传入时间戳
:param _type: 编辑时间戳类型,1为start_time, 2为end_time, 3为年月日
:param
is_timing: 是否为定时作业
:return: 字符串类型的时间串儿
'2018-06-26'
"""
if _type == 0:
# 错误的传入参数, 返回为空
return ''
elif _type == 1:
# 构成 '12-21 24:00(星期4)'
day_of_the_week_dict
= {0: '(星期一)', 1: '(星期二)',
2: '(星期三)', 3: '(星期四)', 4: '(星期五)', 5: '(星期六)', 6: '(星期日)'}
day_of_the_week = timestamp // 86400 % 7
time_array = time.localtime(timestamp)
time_string = time.strftime("%m-%d
%H:%M", time_array)
return time_string + day_of_the_week_dict.get(time_array.tm_wday,
'')
elif _type == 2:
# 构成 '12-21 24:00' 或者 '定时发送12-21 24:00'
time_array = time.localtime(timestamp)
time_string = time.strftime("%m-%d
%H:%M", time_array)
return '定时发送' + time_string if is_timing else time_string
elif _type == 3:
# 构成 '2017年12月'
time_array = time.localtime(timestamp)
time_string = time.strftime("%Y-%m:", time_array).replace('-', '年').replace(':', '月')
return time_string
elif _type == 4:
# 构成 '12月11日'
time_array = time.localtime(timestamp)
time_string = time.strftime("%m-%d:", time_array).replace('-', '月').replace(':', '日')
return time_string + ' '
elif _type == 5:
# 构成 '12月11日 23:00'
time_array = time.localtime(timestamp)
time_string = time.strftime("%m-%d!
%H:%M", time_array).replace('-', '月').replace('!', '日')
return time_string
elif _type == 6:
day_of_the_week_dict
= {0: '(星期一)', 1: '(星期二)',
2: '(星期三)', 3: '(星期四)', 4: '(星期五)', 5: '(星期六)', 6: '(星期日)'}
# 构成 '2017-12-12 12:12'
time_array = time.localtime(timestamp)
time_string = time.strftime("%Y-%m-%d
", time_array)
return time_string+day_of_the_week_dict.get(time_array.tm_wday,
'')
elif _type == 7:
day_of_the_week_dict
= {0: '(星期一)', 1: '(星期二)',
2: '(星期三)', 3: '(星期四)', 4: '(星期五)', 5: '(星期六)', 6: '(星期日)'}
# 构成 '2019年06月20日(星期四)'
time_array = time.localtime(timestamp)
time_string = time.strftime("%Y-%m-%d", time_array)
time_list=time_string.split("-")
time_string="%s年%s月%s日" %(time_list[0],time_list[1],time_list[2])
return time_string+day_of_the_week_dict.get(time_array.tm_wday,
'')
def edit_timestamp(timestamp, is_timing, _type=0):
"""
:param timestamp: 传入时间戳
:param _type: 编辑时间戳类型,1为start_time, 2为end_time, 3为年月日
:param is_timing: 是否为定时作业
:return: 字符串类型的时间串儿 '2018-06-26'
"""
if _type == 0:
# 错误的传入参数, 返回为空
return ''
elif _type == 1:
# 构成 '12-21 24:00(星期4)'
day_of_the_week_dict = {0: '(星期一)', 1: '(星期二)',2: '(星期三)',
3:'(星期四)', 4: '(星期五)', 5: '(星期六)', 6: '(星期日)'}
day_of_the_week = timestamp // 86400 % 7
time_array = time.localtime(timestamp)
time_string = time.strftime("%m-%d %H:%M", time_array)
return time_string + day_of_the_week_dict.get(time_array.tm_wday, '')
elif _type == 2:
# 构成 '12-21 24:00' 或者 '定时发送12-21 24:00'
time_array = time.localtime(timestamp)
time_string = time.strftime("%m-%d %H:%M", time_array)
return '定时发送' + time_string if is_timing else time_string
elif _type == 3:
# 构成 '2017年12月'
time_array = time.localtime(timestamp)
time_string = time.strftime("%Y-%m:", time_array).replace('-', '年').replace(':', '月')
return time_string
elif _type == 4:
# 构成 '12月11日'
time_array = time.localtime(timestamp)
time_string = time.strftime("%m-%d:", time_array).replace('-', '月').replace(':', '日')
return time_string + ' '
elif _type == 5:
# 构成 '12月11日 23:00'
time_array = time.localtime(timestamp)
time_string = time.strftime("%m-%d! %H:%M", time_array).replace('-', '月').replace('!', '日')
return time_string
elif _type == 6:
day_of_the_week_dict = {0: '(星期一)', 1: '(星期二)', 2: '(星期三)',
3: '(星期四)', 4: '(星期五)', 5: '(星期六)', 6: '(星期日)'}
# 构成 '2017-12-12 12:12'
time_array = time.localtime(timestamp)
time_string = time.strftime("%Y-%m-%d ", time_array)
return time_string+day_of_the_week_dict.get(time_array.tm_wday, '')
下载excel 表格处理
FLASK 下
def save_excel_sheets(data_qy, data_ds, data_kh):
"""
功能说明:在一个excel中将区域经理、地市经理、客户经理数据保存到3个sheet中
work_book =
openpyxl.Workbook()
sheet_qy = work_book.create_sheet("区域经理", 0) # sheet1
sheet_ds = work_book.create_sheet("地市经理", 1)
sheet_kh = work_book.create_sheet("客户经理", 2)
for obj_qy in data_qy: # day_qy 一个包含表头的表格数据
sheet_qy.append(obj_qy)
for obj_ds in data_ds:
sheet_ds.append(obj_ds)
for obj_kh in data_kh:
sheet_kh.append(obj_kh)
content = save_virtual_workbook(workbook=work_book)
work_book.close()
response = make_response(content)
response.headers["Content-Disposition"] = f'attachment'
response.headers['Content-Type'] = 'application/octet-stream'
return response
Django 下
try:
wb = Workbook(encoding='utf-8')
qy = wb.add_sheet('区域经理')
ds = wb.add_sheet('地市经理')
kh = wb.add_sheet('客户经理')
for row_i,
row in enumerate(data_qy):
for col_j,
col in enumerate(row):
qy.write(row_i, col_j, label=col)
for row_i,
row in enumerate(data_ds):
for col_j,
col in enumerate(row):
ds.write(row_i, col_j, label=col)
for row_i,
row in enumerate(data_kh):
for col_j,
col in enumerate(row):
kh.write(row_i, col_j, label=col)
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition']
= 'attachment; filename={}.xls'.format(file_name)
wb.save(response)
return response
except Exception
as e:
print e