title: Python datetime: format string %-m (%#m) unexpected behaviour
body: <p>I am trying to format a date in format like '1/1/18' to a datetime object. I think a <a href="https://www.programiz.com/python-programming/datetime/strptime" rel="nofollow noreferrer">proper format string would be '%-m/%-d/%-y'</a> (or '%#m/%#d/%#y' for Windows), but I couldn't confirm it with the <a href="https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes" rel="nofollow noreferrer">official Python docs</a>. I assume its m/d because of other dates I saw in the spreadsheet (5/28/2018, for example).</p>
  <p>I think I have a problem with no-leading-zeros day, month and year. <a href="https://www.programiz.com/python-programming/datetime/strptime" rel="nofollow noreferrer">This site</a> states that &quot;%-m&quot; (or &quot;%#m&quot; for Windows) would give me the month without a leading zero, so January would return 1 instead of 01. Let's try this:</p>
  <pre><code>&gt;&gt;&gt; date_obj= datetime(year=2018, month=1, day=1)
  &gt;&gt;&gt; date_obj
  datetime.datetime(2018, 1, 1, 0, 0)
  &gt;&gt;&gt; date_obj.strftime(&quot;%#m&quot;)
  '1'
  </code></pre>
  <p>Works fine.</p>
  <p>Let's run this through a complete date string.</p>
  <pre><code>&gt;&gt;&gt; format_string = '%#m/%#d/%#y'
  </code></pre>
  <pre><code>&gt;&gt;&gt; date_obj.strftime(format_string)
  '1/1/18'
  </code></pre>
  <p>Works fine too.</p>
  <p>But when I try to parse this string with <code>strptime</code> function:</p>
  <pre><code>&gt;&gt;&gt; datetime.strptime(format_string, '1/1/18')
  Traceback (most recent call last):
    File &quot;&lt;input&gt;&quot;, line 1, in &lt;module&gt;
    File &quot;C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\_strptime.py&quot;, line 568, in _strptime_datetime
      tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    File &quot;C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\_strptime.py&quot;, line 349, in _strptime
      raise ValueError(&quot;time data %r does not match format %r&quot; %
  ValueError: time data '%#m/%#d/%#y' does not match format '1/1/18'
  </code></pre>
  <p>So actually a &quot;circular use&quot; of <code>strptime</code> and <code>strftime</code> results in an exception:</p>
  <pre><code>&gt;&gt;&gt; datetime.strptime(
          format_string, 
          date_obj.strftime(format_string)
      )
  
  Traceback (most recent call last):
    File &quot;&lt;input&gt;&quot;, line 1, in &lt;module&gt;
    File &quot;C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\_strptime.py&quot;, line 568, in _strptime_datetime
      tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    File &quot;C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\_strptime.py&quot;, line 349, in _strptime
      raise ValueError(&quot;time data %r does not match format %r&quot; %
  ValueError: time data '%#m/%#d/%#y' does not match format '1/1/18'
  </code></pre>
  <p>What is a correct approach to converting a date string '1/1/18' to a datetime object?</p>
  <p>I recreated this issue with the IPython console.</p>
  <pre><code>&gt;&gt;&gt; import sys; print('Python %s on %s' % (sys.version, sys.platform))
  Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
  </code></pre>
  <p>PS I'm not responsible for this date format, I know there are better ways of presenting dates. I was just asked to work on the data like this and am trying to make it more readable.</p>