1698537600000转换为%Y-%m-%d格式
时间: 2024-06-13 19:05:58 浏览: 14
```python
import time
timestamp = 1698537600000 / 1000 # 将毫秒转换为秒
date = time.strftime('%Y-%m-%d', time.localtime(timestamp))
print(date) # 输出:2023-06-28
```
--相关问题--:
1. 如何将时间戳转换为日期时间格式?
2. Python
相关问题
python时间戳转%Y-%m-%d
以下是将时间戳转换为%Y-%m-%d格式的Python代码示例:
```python
import time
timestamp = 1635350400 # 时间戳,单位为秒
time_str = time.strftime('%Y-%m-%d', time.localtime(timestamp))
print(time_str) # 输出:2021-10-27
```
代码解释:
1. 首先导入time模块。
2. 定义一个时间戳变量timestamp,这里以1635350400为例,它代表的是2021年10月28日0时0分0秒的时间戳。
3. 使用time.localtime()函数将时间戳转换为本地时间的struct_time对象。
4. 使用time.strftime()函数将struct_time对象格式化为%Y-%m-%d的字符串格式。
5. 最后将格式化后的时间字符串打印输出。
PYTHON 202110081315转换为时间格式'%Y-%m-%d %H%M'
可以使用datetime模块将其转换成时间格式,具体代码如下:
```python
import datetime
time_str = 'PYTHON 202110081315'
time_obj = datetime.datetime.strptime(time_str[7:], '%Y%m%d%H%M')
time_str_format = time_obj.strftime('%Y-%m-%d %H%M')
print(time_str_format)
```
输出结果为:
```
2021-10-08 1315
```
我们首先使用字符串切片取出时间部分,即'202110081315'。然后使用`strptime`方法将其转换为datetime对象。`'%Y%m%d%H%M'`是时间格式的字符串,其中%Y表示年份,%m表示月份,%d表示日期,%H表示小时,%M表示分钟。最后使用`strftime`方法将datetime对象转换为指定格式的字符串。`'%Y-%m-%d %H%M'`中的-和空格是指定的分隔符。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.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)