Convert 12 hour time string into datetime or time
<p>Ive been trying to use <code>time.strptime(string1,'%H:%M')</code>, with no success</p>

<p>How can I get the following:</p>

<pre><code>Input     Output
3:14AM  -&gt; 03:14
9:33PM  -&gt; 21:33
12:21AM -&gt; 00:21
12:15PM -&gt; 12:15
</code></pre>

<p>Use <code>%I</code> for 12 hour times and <code>%p</code> for the <code>am</code> or <code>pm</code> as follows:</p>

<pre><code>from datetime import datetime

for t in ["3:14AM", "9:33PM", "12:21AM", "12:15PM"]:
    print datetime.strptime(t, '%I:%M%p').strftime("%H:%M")
</code></pre>

<p>Giving you the following output:</p>

<pre><code>03:14
21:33
00:21
12:15 
</code></pre>

<p>The formats used are documented in <a href="https://docs.python.org/2/library/datetime.html?highlight=strptime#strftime-strptime-behavior" rel="nofollow">strftime() and strptime() Behavior</a></p>

python datetime time