我在打印从字符串生成的格式化时间对象时遇到问题
这是我的代码:
date_time_str = 'May 10 2021'
date_time_obj = datetime. strptime(date_time_str, '%m %d %Y')
print("The type of the date is now", type(date_time_obj))
This is the error:
ValueError: time data 'May 10 2021' does not match format '%m %d %Y'
最佳答案
根据 This链接,对于 Month
格式的一个月,您需要使用 %B
,对于 Mth
格式的一个月('Apr' ,'Jun') , 使用 %b
.
您使用的是 %m
,它用于数字。
以下为示例:
import time
import datetime
from time import strptime
print("hello world")
date_time_str = 'May 10 2021'
date_time_obj = strptime(date_time_str, '%B %d %Y')
print("The type of the date is now", date_time_obj)
关于python - 时间数据 'May 10 2021' 与格式 '%m %d %Y' 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67930622/