程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python3 UTC and ISO time conversion

編輯:Python

Time stamp date and Date to time stamp

ticks = time.time() # Get the timestamp
print("ticks:",ticks)
localdate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ticks))# Time stamp date
print("localdate:",localdate)
date_to_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(a))# Date to time stamp
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

Time stamping ISO Time

ticks = time.time() # Get the timestamp
localdate = datetime.fromtimestamp(ticks) # Turn the timestamp to datetime.datetime type 2022-05-27 13:40:57.899528
# Or directly datetime.now() obtain datetime.datetime Type date
#astimezone Change the time zone ,isoformat() With iso Format output time string
iso_date = localdate.astimezone().isoformat()
print("ISO Time ",iso_date)

Remove iso Extra characters after

location = iso_date.rfind('.')
date = iso_date[:location]
print(" After removing :",date)
---------------------
ISO Time 2022-05-27T13:50:34.410212+08:00 <class 'str'>
After removing :2022-05-27T13:50:34


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved