time模块使用
1,三个时间对象
-
struct_time: time对象
-
timestamp:浮点数。
-
format_time: 字符换
2,struct_time 相互转换 format_time
- time.strftime(format, struct_time) -> format_time
- time.strptime(format_time, format) -> struct_time
%字符代表变量
- %Y 年变量
- %m 月变量
- %d 日变量
- % H 小时变量
- %M 分钟变量
- %S 秒数变量
import time
# 新建一个当前时间time对象
current_stctime = time.localtime()
# 将time对象转换为格式字符串
current_str = time.strftime('%Y年%m月%d日 %H时%M分%S秒', current_stctime)
'''
2024年01月06日 21时33分05秒
'''
# 将格式字符串转换为time对象
current_stctime = time.strptime(current_str, '%Y年%m月%d日 %H时%M分%S秒')
3, struct_time相互转换timestamp
import time
# 获取浮点数时间戳
timestamp = time.time()
# 时间戳转换为time对象
struct_time = time.localtime(timestamp)
# time对象转换为时间戳
timestamp = time.mktime(struct_time)