2019-03-17 11:00:00格式轉化
import datetime # str轉時間格式: dd = '2019-03-17 11:00:00' dd = datetime.datetime.strptime(dd, "%Y-%m-%d %H:%M:%S") print(dd,type(dd)) # 時間格式轉str: dc = dd.strftime("%Y-%m-%d %H:%M:%S") print(dc,type(dc))
輸出:2019-03-17 11:00:00 <class 'datetime.datetime'>
2019-03-17 11:00:00 <class 'str'>
20190616格式轉化
# str轉時間格式: dd = '20190317' dd = datetime.datetime.strptime(dd, "%Y%m%d") print(dd,type(dd)) # 時間格式轉str: dc = dd.strftime("%Y%m%d") print(dc,type(dc))
注:2019-03-17轉化成20190317
startDate="2019-06-16" startDate=startDate.replace("-","") print(startDate)