代码

    1. import time
    2. #日期转换为时间戳
    3. def timeformatedate(date1):
    4. timeArray = time.strptime(date1, "%Y-%m-%d %H:%M:%S")
    5. timeStamp = int(time.mktime(timeArray))
    6. return timeStamp
    7. #时间戳转换为日期
    8. def dateFormattime(timeStamp):
    9. time_local = time.localtime(timeStamp) # 作用是格式化时间戳为本地的时间
    10. dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
    11. return dt
    12. if __name__ == '__main__':
    13. date = '2022-1-17 13:10:00'
    14. timestamp = timeformatedate(date)
    15. print(timestamp)
    16. timeStamp = time.time()
    17. date = dateFormattime(timeStamp)
    18. print(date)

    执行结果:

    1. 1642396200
    2. 2022-01-17 17:22:45