https://www.runoob.com/python/python-date-time.htmlhtml
當前時間:python
import time now=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(int(time.time())-24*60*60)) print(now)
時間戳轉換爲指定日期:數組
# -*- coding: utf-8 -*- #時間戳轉換爲指定格式日期: import time #timeStamp = 1381419600 timeStamp = int(time.time()) #當前時間戳 timeArray = time.localtime(timeStamp) #本地時間 formatTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) print(formatTime) import datetime #得到當前時間 now = datetime.datetime.now() #這是時間數組格式 formatTime = now.strftime("%Y-%m-%d %H:%M:%S") print(formatTime)
將字符串的時間轉爲時間戳ide
#將字符串的時間轉換爲時間戳 import time now = "2015-11-1 23:30:00" timeArray = time.strptime(now, "%Y-%m-%d %H:%M:%S") timeStamp = int(time.mktime(timeArray)) print(timeStamp) #字符串格式更改: 如now = "2015-11-01 23:30:00",想改成 now = "2015年11月01日 23:40:00" #方法:先轉換爲時間數組,而後轉換爲其餘格式 import time now = "2015-11-01 23:30:00" timeArray = time.strptime(now,"%Y-%m-%d %H:%M:%S") formatTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray) print(formatTime)
計算3天之前:orm
#計算3天之前 import time import datetime threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3)) timeStamp = int(time.mktime(threeDayAgo.timetuple())) formatTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S") print(formatTime)
import time # 將格式字符串轉換爲時間戳 b = time.mktime(time.strptime("2021-10-01 00:00:00", "%Y-%m-%d %H:%M:%S")) # 加1天 c = time.localtime(b + 86400) print(b) print(c) print(time.localtime()) # 將當前時間格式化爲下列格式(2021-07-11 20:28:11) print(time.strftime("%Y-%m-%d %H:%M:%S", c)) print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) # 1633017600.0 # time.struct_time(tm_year=2021, tm_mon=10, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=275, tm_isdst=0) # time.struct_time(tm_year=2021, tm_mon=7, tm_mday=11, tm_hour=20, tm_min=49, tm_sec=42, tm_wday=6, tm_yday=192, tm_isdst=0) # 2021-10-02 00:00:00 # 2021-07-11 20:49:42
當前時間:htm
import time now=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(int(time.time())-24*60*60)) print(now)
時間戳轉換爲指定日期:three
# -*- coding: utf-8 -*- #時間戳轉換爲指定格式日期: import time #timeStamp = 1381419600 timeStamp = int(time.time()) #當前時間戳 timeArray = time.localtime(timeStamp) #本地時間 formatTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) print(formatTime) import datetime #得到當前時間 now = datetime.datetime.now() #這是時間數組格式 formatTime = now.strftime("%Y-%m-%d %H:%M:%S") print(formatTime)
將字符串的時間轉爲時間戳utf-8
#將字符串的時間轉換爲時間戳 import time now = "2015-11-1 23:30:00" timeArray = time.strptime(now, "%Y-%m-%d %H:%M:%S") timeStamp = int(time.mktime(timeArray)) print(timeStamp) #字符串格式更改: 如now = "2015-11-01 23:30:00",想改成 now = "2015年11月01日 23:40:00" #方法:先轉換爲時間數組,而後轉換爲其餘格式 import time now = "2015-11-01 23:30:00" timeArray = time.strptime(now,"%Y-%m-%d %H:%M:%S") formatTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray) print(formatTime)
計算3天之前:字符串
#計算3天之前 import time import datetime threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3)) timeStamp = int(time.mktime(threeDayAgo.timetuple())) formatTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S") print(formatTime)
import time # 將格式字符串轉換爲時間戳 b = time.mktime(time.strptime("2021-10-01 00:00:00", "%Y-%m-%d %H:%M:%S")) # 加1天 c = time.localtime(b + 86400) print(b) print(c) print(time.localtime()) # 將當前時間格式化爲下列格式(2021-07-11 20:28:11) print(time.strftime("%Y-%m-%d %H:%M:%S", c)) print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) # 1633017600.0 # time.struct_time(tm_year=2021, tm_mon=10, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=275, tm_isdst=0) # time.struct_time(tm_year=2021, tm_mon=7, tm_mday=11, tm_hour=20, tm_min=49, tm_sec=42, tm_wday=6, tm_yday=192, tm_isdst=0) # 2021-10-02 00:00:00 # 2021-07-11 20:49:42