使用python 1697361135000 时间戳 转格式化字符串
时间: 2024-06-13 21:05:45 浏览: 10
```python
import time
timestamp = 1697361135000 / 1000 # 时间戳除以1000转换为秒级时间戳
time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))
print(time_str)
```
输出结果为:2023-12-13 22:05:35
相关问题
python 13位时间戳转字符串 字符串里包含毫秒
可以使用Python标准库中的`datetime`模块来实现13位时间戳转换成字符串,并且包含毫秒。具体实现如下:
```python
import datetime
timestamp = 1616122263553 # 13位时间戳
dt = datetime.datetime.fromtimestamp(timestamp / 1000.0) # 转换成datetime对象
str_time = dt.strftime('%Y-%m-%d %H:%M:%S.%f') # 格式化输出,包含毫秒
print(str_time) # 输出:2021-03-19 17:31:03.553000
```
在上面的代码中,我们首先将13位时间戳除以1000,得到的结果是秒数,然后使用`datetime.datetime.fromtimestamp()`方法将其转换成`datetime`对象。接着,使用`strftime()`方法将`datetime`对象格式化成指定的字符串格式,其中`%f`表示毫秒。
最后输出的字符串即为带毫秒的时间字符串。
python时间戳转字符串
你可以使用Python的`datetime`模块来将时间戳转换为字符串。以下是一个示例代码:
```python
import datetime
timestamp = 1627799732 # 你的时间戳
# 使用datetime模块将时间戳转换为datetime对象
dt_object = datetime.datetime.fromtimestamp(timestamp)
# 使用strftime方法将datetime对象格式化为字符串
formatted_string = dt_object.strftime('%Y-%m-%d %H:%M:%S')
print(formatted_string) # 输出:2021-08-01 12:22:12
```
在上面的示例中,我们首先使用`datetime.fromtimestamp()`方法将时间戳转换为`datetime`对象,然后使用`strftime()`方法将`datetime`对象格式化为指定的字符串格式。在`strftime()`方法中,`%Y`表示年份,`%m`表示月份,`%d`表示日期,`%H`表示小时,`%M`表示分钟,`%S`表示秒。
你可以根据自己的需求调整格式化字符串来获得想要的时间字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)