python时间戳转化

一、13位时间戳转化为时间

datatime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(str(1585065600000)[0:10])))
datatime = datatime + '.' + str(1585065600000)[10:]
print(datatime)

 

local_str_time = datetime.datetime.utcfromtimestamp(1585065600000 / 1000.0).strftime('%Y-%m-%d %H:%M:%S.%f')
print(local_str_time)  #2020-03-24 16:00:00.000000

二、获取毫秒级、微秒级等时间戳

import time
import datetime

t = time.time()

print (t)                       #原始时间数据
print (int(t))                  #秒级时间戳
print (int(round(t * 1000)))    #毫秒级时间戳
print (int(round(t * 1000000))) #微秒级时间戳

返回:

1585103405.2433121
1585103405
1585103405243
1585103405243312

三、获取今日0点今日23点59分的时间戳

now = datetime.datetime.now()
# 今日0点
zeroToday = now - datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second,
                                     microseconds=now.microsecond)
# 今日23点59
lastToday = zeroToday + datetime.timedelta(hours=23, minutes=59, seconds=59)
zeroToday = str(zeroToday)
lastToday = str(lastToday)
zeroarray = time.strptime(zeroToday, "%Y-%m-%d %H:%M:%S")
zerostamp = int(time.mktime(zeroarray))
lastarray = time.strptime(lastToday, "%Y-%m-%d %H:%M:%S")
laststamp = int(time.mktime(lastarray))
发布了64 篇原创文章 · 获赞 8 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览