將時(shí)間戳轉(zhuǎn)換為對(duì)應(yīng)時(shí)區(qū)的時(shí)間
import pytz
# 巴西時(shí)間
t=datetime.datetime.fromtimestamp(1559275197261/1000,pytz.timezone('Brazil/East')).strftime('%Y-%m-%d %H:%M:%S')
# 上海時(shí)間
t=datetime.datetime.fromtimestamp(1559275197261/1000,pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
將時(shí)間轉(zhuǎn)化為時(shí)間戳
1.利用strptime()函數(shù)將時(shí)間轉(zhuǎn)換成時(shí)間數(shù)組
2.利用mktime()函數(shù)將時(shí)間數(shù)組轉(zhuǎn)換成時(shí)間戳
import time
dt = '2019-05-20 06:13:14'
# 轉(zhuǎn)化為時(shí)間數(shù)組
dateArray = time.strptime(dt,'%Y-%m-%d %H:%M:%S')
dateStamp = int(time.mktime(dateArray))
兩個(gè)時(shí)間戳算出時(shí)間的差值(單位為秒s)
import time
dt = '2019-05-31 11:59:57'
dt2 = '2019-05-31 10:49:57'
# 轉(zhuǎn)化為時(shí)間數(shù)組
dateArray = time.strptime(dt,'%Y-%m-%d %H:%M:%S')
dateStamp = int(time.mktime(dateArray))
dateArray2 = time.strptime(dt2,'%Y-%m-%d %H:%M:%S')
dateStamp2 = int(time.mktime(dateArray2))
print dateStamp-dateStamp2