python学习-46 时间模块
Posted liujinjing521
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习-46 时间模块相关的知识,希望对你有一定的参考价值。
时间模块
····时间戳
print(time.time())
运行结果:
1 1564294158.0389376 2 3 Process finished with exit code 0
·····结构化时间
print(time.localtime()) # 当地时间 y = time.localtime() print(y.tm_year) print(y.tm_wday)
print(time.gmtime()) # 世界标准化时间 UTC
运行结果:
time.struct_time(tm_year=2019, tm_mon=7, tm_mday=28, tm_hour=14, tm_min=25, tm_sec=57, tm_wday=6, tm_yday=209, tm_isdst=0) 2019 6 time.struct_time(tm_year=2019, tm_mon=7, tm_mday=28, tm_hour=6, tm_min=25, tm_sec=57, tm_wday=6, tm_yday=209, tm_isdst=0) Process finished with exit code 0
·····结构化时间转换成时间戳
print(time.mktime(time.localtime()))
``````结构化时间转换成字符串时间
print(time.strftime(‘%Y-%m-%d %X‘,time.localtime()))
运行结果:
2019-07-28 14:34:58
Process finished with exit code 0
``````字符串时间转换成结构化时间
print(time.strptime(‘2018:1:1:12:00:00‘,‘%Y:%m:%d:%X‘))
运行结果:
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=1, tm_hour=12, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)
Process finished with exit code 0
······把结构化时间转换成固定的字符串表达形式
print(time.asctime()) print(time.ctime())
运行结果:
Sun Jul 28 14:43:45 2019 Sun Jul 28 14:43:45 2019 Process finished with exit code 0
····推迟运行
print(time.sleep(1))
ps:
import datetime模块
print(datetime.datetime.now())
以上是关于python学习-46 时间模块的主要内容,如果未能解决你的问题,请参考以下文章