Python 时间戳和日期相互转换

 

import time

tl = time.localtime()
format_time = time.strftime("%Y-%m-%d %H", tl)
ts = time.strptime(format_time, "%Y-%m-%d %H")
timestamp_end = int(time.mktime(ts))*1000

timestamp_start = timestamp_end-2604800000 # 一个月
print(timestamp_start)
print(timestamp_end)


str_val = int(timestamp_start / 1000)
time_local = time.localtime(str_val)
start_time_trian = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
print(start_time_trian)


# print(endtime_trian)
#
# st = "2021-06-13 07:26:40"
# ts1 = time.strptime(st, "%Y-%m-%d %H:%M:%S")
# timestamp_end1= int(time.mktime(ts1))*1000
# print(timestamp_end1)

 

posted on 2021-07-13 11:17  玩转机器学习  阅读(345)  评论(0编辑  收藏  举报