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 @ 2024-04-20 08:08  木头左  阅读(155)  评论(0编辑  收藏  举报