[00-00] 将 '2018/12/18 10:30' 转换为 '2018-12-18 10:30:00
origin_time = '2018/12/18 10:30'
to_time = time.strptime(origin_time, "%Y/%m/%d %H:%M")
print(time.strftime("%Y-%m-%d %H:%M:%S", to_time))
[00-01] 将 'Wed 06 Mar 2019 17:41' 转换为 '2019-03-06 17:41:00'
1 import time
2 a = 'Wed 06 Mar 2019 17:41'
3 b = "%Y-%m-%d %H:%M:%S"
4 ts = time.strptime(a, "%a %d %b %Y %H:%M")
5 print(ts)
6 # print(time.mktime(ts)) # 得到时间戳
7 format_time = time.strftime(b,ts)
8 print(format_time)
9
10 # 封装成方法
11 def get_default_time_str(event_time):
12 if event_time:
13 st = time.strptime(event_time, "%a %d %b %Y %H:%M")
14 tf = time.strftime("%Y-%m-%d %H:%M:%S", st)
15 return tf
16 else:
17 return ''
参考地址:https://blog.csdn.net/holdlg/article/details/62436537
2. 在当前时间向前推几天或向后推几天
import datetime
# 当前时间日期
now_time = datetime.datetime.now()
# 当前时间的前一周日期时间
START_DATE = (now_time - datetime.timedelta(days=14)).strftime("%Y-%m-%d")
# 当前时间日期
TO_DATE = now_time.strftime("%Y-%m-%d")
# 当前时间的后一周日期时间
# TO_DATE = (now_time + datetime.timedelta(days=7)).strftime("%Y-%m-%d")
请求当前时间
datetime.date.today()
当前时间向后延长 n 天
datetime.date.today()+datetime.tiemdelta(days=n)
将数字转化成时间格式
from dateutil.parser import parse
a=20170825
b=str(a)
c=parse(b)
print(c)
2017-08-25 00:00:00
参考:https://blog.csdn.net/luoganttcc/article/details/77585038
# 时间转换 2019-02-26 18:15 | 2019-02-26 18:15:00
def time_conversion(data_time):
# 转换成时间数组
time_array = time.strptime(data_time, "%Y-%m-%d %H:%M")
# 转换成时间戳
time_stamp = time.mktime(time_array)
# 转换成localtime
time_local = time.localtime(time_stamp)
# 转换成新的时间格式(2016-05-05 20:28:54)
data_time_ = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
return data_time_
当前日期时间 与 两周前的日期时间
import time
import datetime
now_time = datetime.datetime.now()
START_DATE = (now_time - datetime.timedelta(days=14)).strftime("%Y-%m-%d")
today_data = time.strftime("%Y-%m-%d")
print(type(now_time), now_time)
print(type(today_data), today_data)
print(type(START_DATE), START_DATE)