Python中time模块的方法整理

358次阅读
没有评论

Python中time模块的方法整理

1、时间戳time.time当前时间。

2、time.sleep程序暂停三秒钟。

3、time.ctime当前时间。

年月日时分秒。

4、time.localtime()将时间戳转换成元组。

显示当前时间的详细信息。

time.mktime将时间元组转换为时间戳。

time.strftime()#将元组时间转换为字符串形式。

time.strptime()#将字符串转换成元组。

实例

import time
t1 = time.time()
print(t1) #程序至此的执行时间
 
# time.sleep(3) #程序至此暂停3秒
 
t2 = time.time()
print(t2)
 
s = time.ctime(t1)  #当前时间
print(s)
 
#将时间戳转换为元组的形式(当前时间详细信息显示)
loc = time.localtime(t1)
print(loc)
print(loc.tm_hour)  #可调用元组里的具体内容
print(loc.tm_mon)
 
 
#将(时间)元组转为时间戳的形式
loc = time.mktime(loc)
print(loc)  #小数点后清零
 
#将元组时间 转为字符串形式
s = time.strftime('%Y-%m-%d')
print(s)  #以年月日的形式打印
s = time.strftime('%Y-%m-%d %H:%M:%S')
print(s)
 
#将字符串转成元组的方式
t = time.strptime('2021/9/13','%Y/%m/%d')  #第一个参数为时间字符串,第二个参数为待转换的格式
print(t)

以上就是Python中time模块的方法整理,希望对大家有所帮助。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2022-04-27发表,共计717字。
新手QQ群:570568346,欢迎进群讨论 Python51学习