我只是想显示我的生日(年月日)。 然后是ValueError 如“时间数据%r不匹配格式%r”%和 "时间数据' 01/27/1998'与格式'%d/%m/%y'不匹配" 这是我的代码
from datetime import datetime, timedelta
birthday = input('When is your birthday (dd/mm/yyyy)?')
birthday_date = datetime.strptime(birthday, "%d/%m/%y")
print('Birthday : ' + str(birthday_date))
one_day = timedelta(days=1)
birthday_eve = birthday_date - one_day
print('Day before birthday : ' + str(birthday_eve))
这是错误的细节
When is your birthday (dd/mm/yyyy)? 01/27/1998
Traceback (most recent call last):
File "010-input_date.py", line 5, in <module>
birthday_date = datetime.strptime(birthday, "%d/%m/%y")
File
"C:\Users\Kohei\AppData\Local\Programs\Python\Python38\lib\_strptime.py",
line 568,
in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File
"C:\Users\Kohei\AppData\Local\Programs\Python\Python38\lib\_strptime.py",
line 349,
in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data ' 01/27/1998' does not match format '%d/%m/%y'
问题来源StackOverflow 地址:/questions/59386288/how-do-i-have-to-match-the-format-between-time-data-01-27-1998-to-d-m-y
它必须是'%d/%m/%Y',所以替换以下行:
birthday_date = datetime.strptime(birthday, "%d/%m/%y")
:
birthday_date = datetime.strptime(birthday, "%d/%m/%Y")
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。