python中的時間有三種表現(xiàn)形式時間戳、元組、格式化的字符串。
一、時間戳、元組、格式化的字符串的獲取
1.時間戳
time.time()
2.元組
time.localtime()
3.字符串
time.asctime()#?Sun Apr 14 22:15:24 2019
time.ctime()#?Sun Apr 14 22:15:24 2019
datetime.datetime.now()#?2019-04-14
二、三者之間的轉(zhuǎn)化
1.時間戳--->元組
time.gmtime(time.time())
2.元組--->時間戳
time.mktime(time.localtime())
3.元組--->字符串
time.strftime(?'%Y:%M:%d','2019/4/14')
time.asctime(time.localtime())
4.字符串-->元組
time.strpime('2019/4/14','%Y:%M:%d')
5.時間戳--->字符串
datetime.date.fromtimestamp()
三、時間計算
# 當前時間+3天
print(datetime.datetime.now() + datetime.timedelta(3))
# 當前時間-3天
print(datetime.datetime.now() - datetime.timedelta(-3))
# 當前時間+3小時
print(datetime.datetime.now() + datetime.timedelta(hours=3))
# 當前時間+30分鐘
print(datetime.datetime.now() + datetime.timedelta(minutes=30))