[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 timeimport datetimenow_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)