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>The problem requires that you first <code>strptime</code> using <code>%I</code> for the 12 hour time and adding the directive <code>%p</code> for AM or PM to get a time object; altogther <code>'%I:%M%p'</code>. Then use <code>strftime</code> to format the time object into a string:</p>

<hr>

<p><em>Trials:</em></p>

<pre><code>&gt;&gt;&gt; tm = time.strptime('12:33AM', '%I:%M%p')
&gt;&gt;&gt; time.strftime('%H:%M', tm)
'00:33'
&gt;&gt;&gt; tm = time.strptime('9:33PM', '%H:%M%p')
&gt;&gt;&gt; time.strftime('%H:%M', tm)
'09:33'
</code></pre>

<p>Doc reference: <a href="https://docs.python.org/2/library/time.html" rel="nofollow">https://docs.python.org/2/library/time.html</a></p>

python datetime time