import time
"""
日期转时间戳
"""
def unix_time(dt):
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
timestamp = int(time.mktime(timeArray))
return timestamp
"""
时间戳转日期
"""
def custom_time(timestamp):
time_local = time.localtime(timestamp)
dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
return dt
time_now = '2019-02-28 10:23:29'
unix_t = unix_time(time_now)
custom_t = custom_time(unix_t)
print(unix_t)
print(custom_t)
"""
时间用指定格式显示,比如 年-月-日 转 年/月/日
"""
dt = "2020-10-10 22:20:20"
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
customTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print(customTime)
"""
时间用指定格式显示,比如 年/月/日 转 年-月-日
"""
dt = "2020/10/10 22:20:20"
timeArray = time.strptime(dt, "%Y/%m/%d %H:%M:%S")
customTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(customTime)