title: Python ISO 8601 datetime parsing
body: <p>I have different types of <code>ISO 8601</code> formatted date strings, using <code>datetime library</code>, i want to obtain a <code>datetime object</code> from these strings.</p>
  
  <p>Example of the input strings:</p>
  
  <ol>
  <li><code>2017-08-01</code> (1st august 2017)</li>
  <li><code>2017-09</code> (september of 2017)</li>
  <li><code>2017-W20</code> (20th week)</li>
  <li><code>2017-W37-2</code> (tuesday of 37th week)</li>
  </ol>
  
  <p>I am able to obtain the 1st, 2nd and 4th examples, but for 3rd, I get a traceback while trying.</p>
  
  <p>I am using datetime.datetime.strptime function for them in try-except blocks, as follows:</p>
  
  <pre><code>try :
      d1 = datetime.datetime.strptime(date,'%Y-%m-%d')
  except :
      try :
          d1 = datetime.datetime.strptime(date,'%Y-%m')
      except :
          try :
              d1 = datetime.datetime.strptime(date,'%G-W%V')
          except :
              print('Not going through')
  </code></pre>
  
  <p>When i tried the 3rd try block on terminal, here's the error i got</p>
  
  <pre><code>&gt;&gt;&gt; dstr
  '2017-W38'
  &gt;&gt;&gt; dt.strptime(dstr,'%G-W%V')
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "C:\Users\tushar.aggarwal\Desktop\Python\Python3632\lib\_strptime.py", line 565, in _strptime_datetime
      tt, fraction = _strptime(data_string, format)
    File "C:\Users\tushar.aggarwal\Desktop\Python\Python3632\lib\_strptime.py", line 483, in _strptime
      raise ValueError("ISO year directive '%G' must be used with "
  ValueError: ISO year directive '%G' must be used with the ISO week directive '%V' and a weekday directive ('%A', '%a', '%w', or '%u').
  </code></pre>
  
  <p>And this is how i got the 4th case working :</p>
  
  <pre><code>&gt;&gt;&gt; dstr
  '2017-W38-2'
  &gt;&gt;&gt; dt.strptime(dstr,'%G-W%V-%u')
  datetime.datetime(2017, 9, 19, 0, 0)
  </code></pre>
  
  <p>Here is the reference for my code : <a href="https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior" rel="nofollow noreferrer">strptime documentation</a></p>
  
  <p>There are many questions on SO regarding date parsing from <code>ISO 8601</code> formats, but I couldn't find one addressing my issue. Moreover the questions involved are very old and take older versions of python where <code>%G</code>, <code>%V</code> directives in <code>strptime</code> are not available.</p>