注意:本文秒级时间戳的转化,日期时间戳转化可查看
1、当前时间日期转时间戳
import datetimeimport timenow = datetime.datetime.now()print(now)print(now.timetuple())time_stamp = time.mktime(now.timetuple())print(time_stamp) #精确到秒级
2、时间字符串转时间戳
import time
# st = time.strptime('2019-5-1 10:12:20','%Y-%m-%d %H:%M:%S')st = time.strptime('2019-5-1','%Y-%m-%d')print(st)
#time.struct_time(tm_year=2019, tm_mon=5, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=121, tm_isdst=-1)time_stamp = time.mktime(st) #转化成时间戳精确到秒级别print(time_stamp) #1556640000.0
3、当前时间直接生成当前时间戳
import timet = time.time() #获取当前时间print(t)print(int(t)) #生成当前秒级时间戳print(int(round(t*1000))) #生成当前毫秒级时间戳
4.时间戳转时间字符串
import timeltime = time.localtime(1552723974)time_stamp = time.strftime('%Y-%m-%d %H:%M:%S',ltime)print(time_stamp)#格式为str类型