time模塊基本使用
'''
時(shí)間格式:時(shí)間戳:單位秒漫谷,1970年開始 time.time()
格式化的時(shí)間字符串
元組(struct_time)共九個(gè)元素:time.localtime()
世界標(biāo)準(zhǔn)時(shí)間:UTC婴梧, 中國使用:UTC+8, DST:夏令時(shí)
'''
import time
a = time.timezone #東8區(qū)
b = time.time() #獲取時(shí)間戳
c = time.localtime() #元組格式時(shí)間纵朋,結(jié)果為UTC+8時(shí)區(qū)
d = time.gmtime(b) #將時(shí)間戳轉(zhuǎn)換成元組格式拧廊,結(jié)果為TUC時(shí)區(qū)
e = time.mktime(c) #將元組格式轉(zhuǎn)換為秒也即時(shí)間戳格式
f = time.strftime('%Y-%m-%d %H:%M:%S',c) #%Y-%m-%d %H:%M:%S為需要轉(zhuǎn)換成的格式托修,c這個(gè)參數(shù)需是元祖類型的,將元組類型轉(zhuǎn)換成格式化時(shí)間字符串
g = time.strptime('2018-07-18 11:54:46','%Y-%m-%d %H:%M:%S') #將格式化時(shí)間字符串轉(zhuǎn)化為元組類型
h = time.asctime() #將元組時(shí)間轉(zhuǎn)為字符串,當(dāng)未傳入?yún)?shù)時(shí)默認(rèn)傳time.localtime()
i = time.ctime() #將時(shí)間戳轉(zhuǎn)換為字符串格式,當(dāng)未傳入?yún)?shù)時(shí)默認(rèn)傳time.time()
print('時(shí)區(qū):time.timezone--> ',a)
print('獲取當(dāng)前時(shí)間戳:time.time()--> ',b)
print('元組格式當(dāng)前時(shí)間挺勿,UTC+8時(shí)區(qū):time.localtime()--> ',c)
print('時(shí)間戳轉(zhuǎn)換為元組,UTC時(shí)區(qū):time.gmtime(b)--> ',d)
print('元組轉(zhuǎn)換為時(shí)間戳:time.mktime(c)--> ',e)
print('元組轉(zhuǎn)換字符串:time.strftime("%Y-%m-%d %H:%M:%S",c)--> ',f)
print('字符串轉(zhuǎn)化為元組:time.strptime("2018-07-18 11:54:46","%Y-%m-%d %H:%M:%S")--> ',g)
print('元組轉(zhuǎn)化為字符串:time.asctime()--> ',h)
print('時(shí)間戳轉(zhuǎn)化為字符串:time.ctime()--> ',i)
時(shí)區(qū):time.timezone--> -28800
獲取當(dāng)前時(shí)間戳:time.time()--> 1531896245.936
元組格式當(dāng)前時(shí)間玄捕,UTC+8時(shí)區(qū):time.localtime()--> time.struct_time(tm_year=2018, tm_mon=7, tm_mday=18, tm_hour=14, tm_min=44, tm_sec=5, tm_wday=2, tm_yday=199, tm_isdst=0)
時(shí)間戳轉(zhuǎn)換為元組踩蔚,UTC時(shí)區(qū):time.gmtime(b)--> time.struct_time(tm_year=2018, tm_mon=7, tm_mday=18, tm_hour=6, tm_min=44, tm_sec=5, tm_wday=2, tm_yday=199, tm_isdst=0)
元組轉(zhuǎn)換為時(shí)間戳:time.mktime(c)--> 1531896245.0
元組轉(zhuǎn)換字符串:time.strftime("%Y-%m-%d %H:%M:%S",c)--> 2018-07-18 14:44:05
字符串轉(zhuǎn)化為元組:time.strptime("2018-07-18 11:54:46","%Y-%m-%d %H:%M:%S")--> time.struct_time(tm_year=2018, tm_mon=7, tm_mday=18, tm_hour=11, tm_min=54, tm_sec=46, tm_wday=2, tm_yday=199, tm_isdst=-1)
元組轉(zhuǎn)化為字符串:time.asctime()--> Wed Jul 18 14:44:05 2018
時(shí)間戳轉(zhuǎn)化為字符串:time.ctime()--> Wed Jul 18 14:44:05 2018
datetime模塊基本使用
import datetime
a = datetime.datetime.now() #獲取當(dāng)前時(shí)間
b = datetime.datetime.now()+datetime.timedelta(3)
c = datetime.datetime.now()+datetime.timedelta(-3)
d = datetime.datetime.now()+datetime.timedelta(hours=3)
e = datetime.datetime.now()+datetime.timedelta(hours=-3)
f = a.replace(minute=30,hour=10)
print('獲取當(dāng)前時(shí)間:datetime.datetime.now()--> ',a)
print('當(dāng)前時(shí)間往后推3天:datetime.datetime.now()+datetime.timedelta(3)--> ',b)
print('當(dāng)前時(shí)間往前推3天:datetime.datetime.now()+datetime.timedelta(-3)--> ',c)
print('三小時(shí)后:datetime.datetime.now()+datetime.timedelta(hours=3)--> ',d)
print('三小時(shí)前:datetime.datetime.now()+datetime.timedelta(hours=-3)--> ',e)
print('時(shí)間替換:a.replace(minute=30,hour=10)--> ',f)
獲取當(dāng)前時(shí)間:datetime.datetime.now()--> 2018-07-18 15:04:49.309000
當(dāng)前時(shí)間往后推3天:datetime.datetime.now()+datetime.timedelta(3)--> 2018-07-21 15:04:49.309000
當(dāng)前時(shí)間往前推3天:datetime.datetime.now()+datetime.timedelta(-3)--> 2018-07-15 15:04:49.309000
三小時(shí)后:datetime.datetime.now()+datetime.timedelta(hours=3)--> 2018-07-18 18:04:49.309000
三小時(shí)前:datetime.datetime.now()+datetime.timedelta(hours=-3)--> 2018-07-18 12:04:49.309000
時(shí)間替換:a.replace(minute=30,hour=10)--> 2018-07-18 10:30:49.309000