What are the methods of the time module in Python?

WBOY
Release: 2023-05-26 11:12:53
forward
1345 people have browsed it

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)
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!