python中的时间转换,秒级时间戳转string,string转时间

import time
import datetime


def paserTime(timestamp):
    t = time.time()
    f=time.localtime(timestamp/1000)
    print (t)                       #原始时间数据
    # print (int(t))                  #秒级时间戳
    print (int(round(t * 1000)))    #毫秒级时间戳

    #nowTime = lambda: int(round(t * 1000))
  #  print(nowTime());  # 毫秒级时间戳,基于lambda
    nowTime = lambda:timestamp
    str=time.strftime('%Y-%m-%d %H:%M:%S',f)
    print(str)  # 日期格式化

    return str
# paserTime(time.time())
def now():
    nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')  # 现在
    print(nowTime)
def nowTime():
    t = time.time()
    f = time.localtime(int(t))
    # print(t)
    str = time.strftime('%Y-%m-%d %H:%M:%S', f)
    print(str)  # 日期格式化
    return str
# nowTime()
def long2Str(longTime):
    f = time.localtime(int(longTime))
    # print(t)
    str = time.strftime('%Y-%m-%d %H:%M:%S', f)
    print(str)  # 日期格式化
    return str
def str2time(str):
    date_time = datetime.datetime.strptime(str, '%Y-%m-%d %H:%M:%S')
    # print(date_time)
    return date_time

def str2timestamp(str):
    timstamp=time.mktime(time.strptime(str, '%Y-%m-%d %H:%M:%S'))
    # print(timstamp)
    return timstamp
    # time.mktime()   与    time.localtime() 互为还原函数。
    # time.mktime(timetuple) :将时间元组转换成时间戳
    # time.localtime([timestamp]):将时间戳转会为时间元组
# print(str2timestamp(str2)-str2timestamp(str))```


 


![](https://mutouzuo.oss-cn-hangzhou.aliyuncs.com/my/mudouzuo1.png)
posted @   木头左  阅读(170)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示