Why does python's datetime.datetime.strptime('201412', '%Y%m%d') not raise a ValueError?
<p>In the format I am given, the date 2014-01-02 would be represented by "20140102".  This is correctly parsed with the standard strptime:</p>

<pre><code>&gt;&gt;&gt; datetime.datetime.strptime("20140102", "%Y%m%d")
datetime.datetime(2014, 1, 2, 0, 0)
</code></pre>

<p>In this format, "201412" would <em>not</em> be a valid date.  The <a href="https://docs.python.org/2/library/time.html">docs</a> say that the "%m" directive is "Month as a zero-padded decimal number."    It gives as examples "01, 02, ..., 12".  The days directive "%d" is also supposed to be  zero-padded.</p>

<p>Based on this, I expected that "201412" would be an invalid input with this format, so would raise a ValueError.  Instead, it is interpreted as 2014-01-02:</p>

<pre><code>&gt;&gt;&gt; datetime.datetime.strptime("201412", "%Y%m%d")
datetime.datetime(2014, 1, 2, 0, 0)
</code></pre>

<p>The question is: is there a way to specify "no seriously zero-padded only"?  Or am I misunderstanding the term "zero-padded" in this context?</p>

<p>Note that the question is not about how to parse dates in this format, but about understanding the behavior of strptime.</p>

<p>According to the related <a href="https://bugs.python.org/issue22840">issue</a> on the Python tracker, with the example being like such (a bit of a modification to this question, however the concept is the exact same):</p>

<pre><code>&gt;&gt;&gt; datetime.datetime.strptime('20141110', '%Y%m%d').isoformat()
'2014-11-10T00:00:00'
&gt;&gt;&gt; datetime.datetime.strptime('20141110', '%Y%m%d%H%M').isoformat()
'2014-01-01T01:00:00'
</code></pre>

<p>The above behavior is determined to be <strong>not</strong> a bug as explained by <a href="https://bugs.python.org/issue22840#msg230991">this comment</a> which states that they conform to the <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/strptime.html">OpenGroup strptime standard</a> which specifies that "leading zeros are permitted but not required.".</p>

<p>I guess the workaround is to use regex or check that the length of the string is of length 8 before passing into <code>strptime</code>.</p>

python datetime strptime