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

Mutual conversion of Python timestamp and date

編輯:Python


Python Mutual conversion of timestamp and date

Use the library ​​time​

import time


print(time.time()) #1643195041.575819 Returns the current timestamp float
# time.time() The time stamp indicates that the 1970 year 1 month 1 Japan 00:00:00 Start offset in seconds .

print(time.localtime()) # Local time zone struct_time( Beijing time. )
# time.struct_time(tm_year=2022, tm_mon=1, tm_mday=26, tm_hour=23, tm_min=56, tm_sec=5, tm_wday=2, tm_yday=26, tm_isdst=0) And time,time() The timestamp corresponds to


print(time.gmtime()) # UTC Time zone struct_time( GMT )
# time.struct_time(tm_year=2022, tm_mon=1, tm_mday=26, tm_hour=15, tm_min=56, tm_sec=5, tm_wday=2, tm_yday=26, tm_isdst=0) from 1970.1.1 08:00:00 Count up



print(time.localtime(time.time())) # Return a date structure time.struct_time(tm_year=2022, tm_mon=1, tm_mday=26, tm_hour=19, tm_min=5, tm_sec=23, tm_wday=2, tm_yday=26, tm_isdst=0)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

1. Reciprocal transformation

print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))

# 2022-01-27
print(time.strptime('2001-07-18','%Y-%m-%d'))
# time.struct_time(tm_year=2001, tm_mon=7, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=199, tm_isdst=-1)
  • 1.
  • 2.
  • 3.
  • 4.




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