Python_时间戳转换 发表于 2017-09-27 | 更新于 2019-06-13 | 分类于 Python Python_时间戳转换12345678910111213141516171819202122232425262728293031323334353637383940414243import timedef time_transform_to_timestamp(date_time, time_format=None): """ 时间转换为时间戳格式 Parameters ---------- date_time 待转换的时间信息,%Y-%m-%d_%H-%M-%S格式,如2018-07-25_17-22-30 Returns 时间戳 ------- """ if time_format is None: time_format = "%Y-%m-%d_%H-%M-%S" time_array = time.strptime(date_time, time_format) timestamp = int(time.mktime(time_array)) return timestampdef timestamp_transform_to_time(timestamp, time_format=None): """ 时间戳转换为格式时间 Parameters ---------- timestamp 待转换的时间戳 Returns 格式时间, 默认%Y-%m-%d_%H-%M-%S格式,如2018-07-25_17-22-30 ------- """ time_array = time.localtime(timestamp) if time_format is None: time_format = "%Y-%m-%d_%H-%M-%S" format_time = time.strftime(time_format, time_array) return format_timeif __name__ == '__main__': timestamp = time_transform_to_timestamp('2018-07-25_17-22-30') print(timestamp) 1532510550 12