strptime 不理解 [=10AM 和 12:30 PM 之间的区别
strptime does not understand difference betwen 12:30 AM and 12:30 PM
下面是 2 个带 AM/PM 的日期时间字符串。包含字符串 returns 的 AM 与包含字符串的 PM 完全相同的日期时间对象值。我做错了什么?
from datetime import datetime
format ='%d %b %y %H:%M %p'
date_str ='23 Feb 22 12:57 PM'
print(datetime.strptime(date_str, format))
# 2022-02-23 12:57:00
date_str ='23 Feb 22 12:57 AM'
print(datetime.strptime(date_str, format))
# 2022-02-23 12:57:00
每https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
When used with the strptime() method, the %p directive only affects the output hour field if the %I directive is used to parse the hour.
>>> format ='%d %b %y %I:%M %p'
>>> date_str ='23 Feb 22 12:57 AM'
>>> print(datetime.strptime(date_str, format))
2022-02-23 00:57:00
>>> date_str ='23 Feb 22 12:57 PM'
>>> print(datetime.strptime(date_str, format))
2022-02-23 12:57:00
下面是 2 个带 AM/PM 的日期时间字符串。包含字符串 returns 的 AM 与包含字符串的 PM 完全相同的日期时间对象值。我做错了什么?
from datetime import datetime
format ='%d %b %y %H:%M %p'
date_str ='23 Feb 22 12:57 PM'
print(datetime.strptime(date_str, format))
# 2022-02-23 12:57:00
date_str ='23 Feb 22 12:57 AM'
print(datetime.strptime(date_str, format))
# 2022-02-23 12:57:00
每https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
When used with the strptime() method, the %p directive only affects the output hour field if the %I directive is used to parse the hour.
>>> format ='%d %b %y %I:%M %p'
>>> date_str ='23 Feb 22 12:57 AM'
>>> print(datetime.strptime(date_str, format))
2022-02-23 00:57:00
>>> date_str ='23 Feb 22 12:57 PM'
>>> print(datetime.strptime(date_str, format))
2022-02-23 12:57:00