This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: datetime.strptime incorrectly handling hours and minutes with bad format string
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8
Status: closed Resolution:
Dependencies: Superseder: strptime %d handling of single digit day of month
View: 34903
Assigned To: Nosy List: joxtoby27
Priority: normal Keywords:

Created on 2021-11-23 19:11 by joxtoby27, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg406877 - (view) Author: Jon Oxtoby (joxtoby27) Date: 2021-11-23 19:11
Running datetime.strptime against a string containing year, month, day but with a formatter including %H causes a two-digit day to be split across the day and hour fields of the datetime:

datetime.datetime.strptime('20140812', '%Y%m%d%H')
returns: datetime.datetime(2014, 8, 1, 2, 0)
expected: ValueError: time data '20140812' does not match format '%Y%m%d%H'

datetime.datetime.strptime('2014081201', '%Y%m%d%H%M')
returns: datetime.datetime(2014, 8, 12, 0, 1)
expected: ValueError: time data '2014081201' does not match format '%Y%m%d%H%M'
msg406879 - (view) Author: Jon Oxtoby (joxtoby27) Date: 2021-11-23 19:45
Just noticed the potential for this issue was raised in so it's possible this is not a bug and is the desired behavior, but the documentation doesn't seem to address the issue (unless I'm overlooking it).
msg407051 - (view) Author: Jon Oxtoby (joxtoby27) Date: 2021-11-26 14:15
I was indeed overlooking the note in the documentation that the leading zero is optional for some formatters when using strptime. Closing.
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 90042
2021-11-26 14:15:36joxtoby27setstatus: open -> closed

messages: + msg407051
stage: resolved
2021-11-26 10:36:29christian.heimessetsuperseder: strptime %d handling of single digit day of month
2021-11-23 19:45:44joxtoby27setmessages: + msg406879
2021-11-23 19:11:12joxtoby27create