目錄html
time.time()
time.localtime()
time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
import time current_timastamp = time.time() 1558150946.8137608
time.struct_time()
time.localtime()
參數默認爲當前的時間戳 time.time()
,能夠指定的時間戳獲取指定的時間元組python
# 獲取當前的時間元組 time.localtime() time.struct_time(tm_year=2019, tm_mon=5, tm_mday=18, tm_hour=11, tm_min=52, tm_sec=13, tm_wday=5, tm_yday=138, tm_isdst=0) # 根據指定的時間戳獲取指定時間元組 time.localtime(current_timastamp) time.struct_time(tm_year=2019, tm_mon=5, tm_mday=18, tm_hour=11, tm_min=42, tm_sec=26, tm_wday=5, tm_yday=138, tm_isdst=0)
經過 time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
轉換爲字符串時間。code
time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())) '2019-05-18 12:04:11'
timestamp ---> struct_time ---> string
time.strptime()
將字符串時間轉換爲時間元祖time.mktime()
時間元祖轉換爲時間戳經過 time.strptime('str_time',"%Y-%m-%d %H:%M:%S")
獲取時間元組 time.struct_time()
htm
str_time = '2019-05-18 03:27:45' # 獲取時間元組 time.strptime(str_time,"%Y-%m-%d %H:%M:%S") time.struct_time(tm_year=2019, tm_mon=5, tm_mday=18, tm_hour=12, tm_min=17, tm_sec=18, tm_wday=5, tm_yday=138, tm_isdst=-1)
經過 time.mktime(struct_time,"%Y-%m-%d %H:%M:%S")
轉換爲時間戳對象
time.mktime(time.strptime(str_time,"%Y-%m-%d %H:%M:%S")) 1558153038.0
總結:blog
time.strptime(str,fomart)
time.mktime(struct_time)
time.localtime(time.time())
與 time.gmtime(time.time())
的區別字符串
time.localtime() time.struct_time(tm_year=2019, tm_mon=5, tm_mday=18, tm_hour=11, tm_min=56, tm_sec=52, tm_wday=5, tm_yday=138, tm_isdst=0) # tm_hour = 11 time.gmtime() time.struct_time(tm_year=2019, tm_mon=5, tm_mday=18, tm_hour=3, tm_min=56, tm_sec=58, tm_wday=5, tm_yday=138, tm_isdst=0) # tm_hour = 8
import datetime d = datetime.datetime.now() datetime.datetime(2019, 6, 14, 22, 37, 50, 634343)
from datetime import datetime # 獲取當前的時間日期 datetime.now() datetime.datetime(2019, 5, 21, 9, 53, 28, 875595) # 將當前的轉換爲字符串時間 datetime.now().strftime('%Y-%m-%d %H:%M:%S') '2019-05-21 09:43:21'
# 字符串時間 time_string = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # '2019-05-23 11:22:36' datetime.datetime.strptime(time_string,'%Y-%m-%d %H:%M:%S') datetime.datetime(2019, 5, 23, 14, 31, 28)
t = time.time() # 1558404024.8977106 datetime.fromtimestamp(t) datetime.datetime(2019, 5, 21, 10, 0, 24, 897711)
timestamp()
t = datetime.now() t.timestamp() 1558404314.672059
# python datetime獲取幾分鐘、小時、天以前的時間 print((datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M")) print((datetime.datetime.now() - datetime.timedelta(minutes=1)).strftime("%Y-%m-%d %H:%M")) print((datetime.datetime.now() - datetime.timedelta(seconds=1)).strftime("%Y-%m-%d %H:%M"))
本文參考:get
博主renpingshengstring
博主清明-心若淡定it