标签:ptime 字符 alt %s port cal str class col
时间模块里面有时间戳、格式化时间对象、时间字符串
import time
# 时间戳
print(time.time()) # 1587642175.403718
?# 格式化时间对象
print(time.gmtime()) # 格林尼治时间对象 print(time.localtime()) # 本地时间对象
显示结果:
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=23, tm_hour=11, tm_min=45, tm_sec=52, tm_wday=3, tm_yday=114, tm_isdst=0)
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=23, tm_hour=19, tm_min=45, tm_sec=52, tm_wday=3, tm_yday=114, tm_isdst=0)
# 时间字符串:
print(time.strftime(‘Year:%Y Month:%m Day:%d %H:%M:%S‘)) # Year:2020 Month:04 Day:23 19:49:52 print(time.strftime(‘%Y-%m-%d %H:%M:%S‘)) # 2020-04-23 19:51:01
时间戳、格式化时间对象、时间字符串之间的转换
# 时间戳 ------> 格式化时间对象 gmtime localtimeprint(time.localtime(time.time())) # time.struct_time(tm_year=2020, tm_mon=4, tm_mday=23, tm_hour=19, tm_min=52, tm_sec=38, tm_wday=3, tm_yday=114, tm_isdst=0)
# 格式化时间对象 ------------> 时间戳 mktimeprint(time.mktime(time.localtime())) # 1587643045.0
# 格式化时间对象 ------------ > 时间字符串 strftimeprint(time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime())) # 2020-04-23 19:59:33
# 时间字符串 ---------------> 格式化时间对象 strptime# time.struct_time(tm_year=2020, tm_mon=10, tm_mday=20, tm_hour=18, tm_min=20, tm_sec=20, tm_wday=1, tm_yday=294, tm_isdst=-1)print(time.strptime(‘2020-10-20 18:20:20‘,"%Y-%m-%d %H:%M:%S"))
--------------------- end ---------------------------------------
标签:ptime 字符 alt %s port cal str class col
原文地址:https://www.cnblogs.com/quanag/p/12763274.html