时间戳与时间类型转化(秒级时间戳)

注意:本文秒级时间戳的转化,日期时间戳转化可查看https://www.cnblogs.com/pinpin/p/10795037.html

1、当前时间日期转时间戳

import datetime
import time

now = 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 time

t = time.time()  #获取当前时间
print(t)
print(int(t)) #生成当前秒级时间戳
print(int(round(t*1000))) #生成当前毫秒级时间戳

4.时间戳转时间字符串

import time

ltime = time.localtime(1552723974)
time_stamp  = time.strftime('%Y-%m-%d %H:%M:%S',ltime)
print(time_stamp)#格式为str类型
posted @ 2019-04-30 11:35  阳光宝贝-沐沐  阅读(6247)  评论(0编辑  收藏  举报