當(dāng)前時(shí)間
1、獲得當(dāng)前時(shí)間戳
print(int(time.time()))
2、格式化輸出當(dāng)前時(shí)間
import time
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
print(t)
時(shí)間戳轉(zhuǎn)格式化字符串
import datetime
dateArray = datetime.datetime.utcfromtimestamp(1581598281)
t = dateArray.strftime('%Y-%m-%d %H:%M:%S')
print(t)
13位時(shí)間戳獲取方法
import time
millis = int(round(time.time() * 1000))
print(millis)
datetime時(shí)間類的轉(zhuǎn)換
import datetime
import time
# 日期時(shí)間字符串
st = "2017-11-23 16:10:10"
# 當(dāng)前日期時(shí)間
dt = datetime.datetime.now()
# 當(dāng)前時(shí)間戳
sp = time.time()
# 1.把datetime轉(zhuǎn)成字符串
def datetime_toString(dt):
print("1.把datetime轉(zhuǎn)成字符串: ", dt.strftime("%Y-%m-%d %H:%M:%S"))
# 2.把字符串轉(zhuǎn)成datetime
def string_toDatetime(st):
print("2.把字符串轉(zhuǎn)成datetime: ", datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S"))
# 3.把字符串轉(zhuǎn)成時(shí)間戳形式
def string_toTimestamp(st):
print("3.把字符串轉(zhuǎn)成時(shí)間戳形式:", time.mktime(time.strptime(st, "%Y-%m-%d %H:%M:%S")))
# 4.把時(shí)間戳轉(zhuǎn)成字符串形式
def timestamp_toString(sp):
print("4.把時(shí)間戳轉(zhuǎn)成字符串形式: ", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(sp)))
# 5.把datetime類型轉(zhuǎn)外時(shí)間戳形式
def datetime_toTimestamp(dt):
print("5.把datetime類型轉(zhuǎn)外時(shí)間戳形式:", time.mktime(dt.timetuple()))
# 1.把datetime轉(zhuǎn)成字符串
datetime_toString(dt)
# 2.把字符串轉(zhuǎn)成datetime
string_toDatetime(st)
# 3.把字符串轉(zhuǎn)成時(shí)間戳形式
string_toTimestamp(st)
# 4.把時(shí)間戳轉(zhuǎn)成字符串形式
timestamp_toString(sp)
# 5.把datetime類型轉(zhuǎn)外時(shí)間戳形式
datetime_toTimestamp(dt)