python 时间戳和时间格式互相转换

#!/usr/bin/python3
# -*- coding: utf-8 -*

import time


def str_to_stamp():
    # 转换显示格式
    time1 = time.strptime(time_str, '%Y-%m-%d %H:%M:%S')
    # 转为时间戳
    time2 = int(time.mktime(time1))
    print(time2)


def stamp_to_str():
    # 转换本地时间
    time1 = time.localtime(time_stamp)
    # 转为时间格式
    time2 = time.strftime("%Y-%m-%d %H:%M:%S", time1)
    print(time2)


if __name__ == '__main__':
    time_str = '2018-7-02 10:58:12'
    time_stamp = 1530500292

    str_to_stamp()
    stamp_to_str()
 
原文地址:https://www.cnblogs.com/37yan/p/10710775.html