1. 程式人生 > >python3 時間戳和時間的一些轉化

python3 時間戳和時間的一些轉化

字符 color python 直接 time print 時間 轉化 時間戳

1.時間日期轉時間戳

import datetime
now = datetime.datetime.now()#datatime類型
time_stamp = time.mktime(now.timetuple())
print(int(time_stamp))#秒級

2.時間字符串轉時間戳

st = time.strptime(2018-10-07 16:12:54, %Y-%m-%d %H:%M:%S)
time_stamp = time.mktime(st) 
print(int(time_stamp))#秒級

3.直接生成當前時間戳

import
time time_stamp = int(time.time()) print(time_stamp)#int

4.時間戳轉時間字符串

ltime = time.localtime(1552723974)
time_stamp  = time.strftime(%Y-%m-%d %H:%M:%S,ltime)
print(time_stamp)#str

python3 時間戳和時間的一些轉化