Home  >  Article  >  Backend Development  >  What are the methods of the time module in Python?

What are the methods of the time module in Python?

WBOY
WBOYforward
2023-05-26 11:12:531482browse

1. Timestamp time.time current time.

2. The time.sleep program pauses for three seconds.

3. time.ctime current time.

Year, month, day, hour, minute and second.

4. time.localtime() converts the timestamp into a tuple.

Display detailed information of the current time.

time.mktime converts time tuples into timestamps.

time.strftime()#Convert tuple time to string form.

time.strptime()#Convert string into tuple.

Example

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)

The above is the detailed content of What are the methods of the time module in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete