python time模块怎么用
时间: 2023-07-06 15:10:48 浏览: 93
`time` 模块提供了一些有用的函数,可以用来获取当前时间和日期,操作时间和日期,以及格式化时间和日期等。下面是一些 `time` 模块的常用函数:
1. `time()`:返回当前时间的时间戳(从1970年1月1日午夜开始按秒计算的浮点数)。
```python
import time
print(time.time())
```
2. `ctime()`:把一个时间戳转换为当前时区的格式化日期时间字符串。
```python
import time
t = time.time()
print(time.ctime(t))
```
3. `gmtime()`:把一个时间戳转换为 UTC(世界标准时间)下的 struct_time(即 struct_time 的 UTC 版本)。
```python
import time
t = time.time()
utc_time = time.gmtime(t)
print(utc_time)
```
4. `localtime()`:把一个时间戳转换为当前时区的 struct_time。
```python
import time
t = time.time()
local_time = time.localtime(t)
print(local_time)
```
5. `strftime()`:把 struct_time 或表示时间的元组转化为格式化的时间字符串。
```python
import time
t = time.localtime()
time_str = time.strftime("%Y-%m-%d %H:%M:%S", t)
print(time_str)
```
6. `strptime()`:把格式化的时间字符串转化为 struct_time。
```python
import time
time_str = "2021-11-01 09:30:00"
t = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")
print(t)
```
还有其他很多函数和用法,这里只是列举了一些常用的。可以查看 Python 官方文档中的 `time` 模块文档了解更多信息。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)