import time
# 获取当前时间的时间戳(以秒为单位)
current_timestamp = time.time()
print(f"当前时间戳: {current_timestamp}")
# 将时间戳转换为可读的本地时间
local_time = time.localtime(current_timestamp)
readable_time = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
print(f"可读时间: {readable_time}")
# 将可读时间转换回时间戳
time_string = "2023-10-05 14:30:00"
time_struct = time.strptime(time_string, "%Y-%m-%d %H:%M:%S")
timestamp_from_string = time.mktime(time_struct)
print(f"从字符串转换的时间戳: {timestamp_from_string}")
time.time()
返回自纪元(通常是1970年1月1日)以来的秒数,即时间戳。time.localtime()
将时间戳转换为本地时间的结构化对象,再通过 time.strftime()
格式化为可读的字符串。time.strptime()
将格式化的字符串解析为时间结构体,再通过 time.mktime()
转换为时间戳。上一篇:python 写文件
下一篇:python 创建文件夹
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站