How to convert string date with timezone to datetime?
<p>I have date in string:</p>

<pre><code>Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)
</code></pre>

<p>and I use (according to <a href="https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior" rel="nofollow">https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior</a>):</p>

<pre><code>datetime.strptime(datetime_string, '%a %b %m %Y %H:%M:%S %z %Z')
</code></pre>

<p>but I get error:</p>

<pre><code>ValueError: 'z' is a bad directive in format '%a %b %m %Y %H:%M:%S %z %Z'
</code></pre>

<p>How to do it correctly?</p>

<p>%z is the <code>+0200</code>, <code>%Z</code> is <code>CEST</code>. Therefore:</p>

<pre><code>&gt;&gt;&gt; s = "Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)"
&gt;&gt;&gt; datetime.strptime(s, '%a %b %d %Y %H:%M:%S GMT%z (%Z)')
datetime.datetime(2016, 10, 4, 12, 13, tzinfo=datetime.timezone(datetime.timedelta(0, 7200), 'CEST'))
</code></pre>

<p>I also replaced your <code>%m</code> with <code>%d</code>; <code>%m</code> is the month, numerically, so in your case <code>04</code> would be parsed as April.</p>

python date