代码
import time
#日期转换为时间戳
def timeformatedate(date1):
timeArray = time.strptime(date1, "%Y-%m-%d %H:%M:%S")
timeStamp = int(time.mktime(timeArray))
return timeStamp
#时间戳转换为日期
def dateFormattime(timeStamp):
time_local = time.localtime(timeStamp) # 作用是格式化时间戳为本地的时间
dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
return dt
if __name__ == '__main__':
date = '2022-1-17 13:10:00'
timestamp = timeformatedate(date)
print(timestamp)
timeStamp = time.time()
date = dateFormattime(timeStamp)
print(date)
执行结果:
1642396200
2022-01-17 17:22:45