時間戳轉日期and日期轉時間戳
ticks = time.time() #獲取時間戳
print("ticks:",ticks)
localdate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ticks))#時間戳轉日期
print("localdate:",localdate)
date_to_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(a))#日期轉時間戳
print("date_to_time:",date_to_time)
----------------------------------
ticks: 1653623433.5224512
localdate: 2022-05-27 11:50:33
date_to_time: 2022-05-25 11:50:33
時間戳轉ISO時間
ticks = time.time() #獲取時間戳
localdate = datetime.fromtimestamp(ticks) #將時間戳轉為datetime.datetime類型 2022-05-27 13:40:57.899528
#或者直接 datetime.now() 獲取 datetime.datetime類型日期
#astimezone修改時區,isoformat()以iso格式輸出時間字符串
iso_date = localdate.astimezone().isoformat()
print("ISO時間",iso_date)
去除iso後面的多余字符
location = iso_date.rfind('.')
date = iso_date[:location]
print("去除後:",date)
---------------------
ISO時間 2022-05-27T13:50:34.410212+08:00 <class 'str'>
去除後:2022-05-27T13:50:34