time
基础概念
常用的日期格式
时间戳:
1654850734.8052504
带格式的字符串:
2022_06_10-16:46:12
时间元组:
time.struct_time(
tm_year=2022,tm_mon=6,tm_mday=10,
tm_hour=16,tm_min=46,tm_sec=27,
tm_wday=4, tm_yday=161, tm_isdst=0)
时间获取
时间戳
- 获取方式
time.time()
- 关系转换
带格式的字符串
- 获取方式
time_format = '%Y_%m_%d-%H:%M:%S' # 定义一个格式
strftime = time.strftime(time_format)
# or
time.strftime("%Y-%m-%d %X", time.localtime())
# 2022_06_10-16:53:11
- 关系转换
# 转换成时间元组
time_format = '%Y_%m_%d-%H:%M:%S'
struct_time = time.strptime(t1, time_format)
#struct_time(
# tm_year=2022,tm_mon=6,tm_mday=10,
# tm_hour=16,tm_min=46,tm_sec=27,
# tm_wday=4, tm_yday=161, tm_isdst=0)
# 转换成时间戳
t = time.mtime(struct_time)
# 1654851267.0
时间元组
- 获取方式
struct_time = time.localtime()
# or
time.localtime(time.time())
# or
struct_time = time.strptime(strftime, time_format)