原文:https://www.cnblogs.com/skyxiuli/p/10778907.html
参考:https://www.cnblogs.com/qiuyi21/archive/2008/03/04/1089456.html
import time#把格式化时间转换成时间戳def str_to_timestamp(str_time=None, format='%Y-%m-%d %H:%M:%S'):if str_time:time_tuple = time.strptime(str_time, format) # 把格式化好的时间转换成元祖result = time.mktime(time_tuple) # 把时间元祖转换成时间戳return int(result)return int(time.time())print(str_to_timestamp('2019-04-27 07:01:46'))print(str_to_timestamp()) #1556349904# 把时间戳转换成格式化def timestamp_to_str(timestamp=None, format='%Y-%m-%d %H:%M:%S'):if timestamp:time_tuple = time.localtime(timestamp) # 把时间戳转换成时间元祖result = time.strftime(format, time_tuple) # 把时间元祖转换成格式化好的时间return resultelse:return time.strptime(format)print(timestamp_to_str(1556348505)) #2019-04-27 15:01:45
