如何将2022-10-27T00:00.000+000:00转换为YYYY-MM-DD格式的标准日期



有人能解释一下如何在python 中将这个2022-10-27T00:00.000+000:00日期字符串值转换为YYYY-MM-DD格式吗

输出应该是这样的:2022-10-27

有人能在这里帮我吗

您的字符串似乎是ISO 8601日期时间字符串。在Python中,解析这样一个字符串的正确方法是使用datetime模块:

from datetime import datetime
old = "2022-10-27T00:00:00.000+00:00"
dt_obj = datetime.fromisoformat(old)
new = dt_obj.strftime("%Y-%m-%d")
print(new)   # <- 2022-10-27

如果是字符串,请使用

variableHoldingString[0:10]

参考编号:https://thispointer.com/python-how-to-get-first-n-characters-in-a-string/#:~:text=到%20get%20the%20first%20N,%20end_index_pos%20as%20N%20i.e.&text=%20step_size%20的%20值%20将,字符%20of%20给定的%20字符串。

相关内容

最新更新