Converting time string 11:00am/pm into UTC in 24 hour format
<p>I have time string <code>11:15am</code> or <code>11:15pm</code>.
I am trying to convert this string into UTC timezone with 24 hour format.</p>

<p>FROM EST to UTC</p>

<p>For example: When I pass <code>11:15am</code> It should convert into <code>15:15</code> and when I pass <code>11:15pm</code> then it should convert to <code>3:15</code>.</p>

<p>I have this code which I am trying:</p>

<pre><code>def appointment_time_string(time_str):
    import datetime
    a = time_str.split()[0]

    # b =  re.findall(r"[^\W\d_]+|\d+",a)

    # c = str(int(b[0]) + 4) + ":" + b[1]
    # print("c", c)



    in_time = datetime.datetime.strptime(a,'%I:%M%p')

    print("In Time", in_time)

    start_time = str(datetime.datetime.strftime(in_time, "%H:%M:%S"))

    print("Start TIme", start_time)

    if time_str.split()[3] == 'Today,':
        start_date = datetime.datetime.utcnow().strftime("%Y-%m-%dT")
    elif time_str.split()[3] == 'Tomorrow,':
        today = datetime.date.today( )
        start_date = (today + datetime.timedelta(days=1)).strftime("%Y-%m-%dT")

    appointment_time = str(start_date) + str(start_time)

    return appointment_time

x = appointment_time_string(time_str)
print("x", x)
</code></pre>

<p>But this is just converting to 24 hour not to UTC.</p>

<p>To convert the time from 12 hours to 24 hours format, you may use below code:</p>

<pre><code>from datetime import datetime
new_time = datetime.strptime('11:15pm', '%I:%M%p').strftime("%H:%M")
# new_time: '23:15'
</code></pre>

<p>In order to convert time from <code>EST</code> to <code>UTC</code>, the most reliable way is to use third party library <a href="http://pytz.sourceforge.net/" rel="nofollow"><code>pytz</code></a>. Refer <a href="http://stackoverflow.com/a/5491705/2063361">How to convert EST/EDT to GMT?</a> for more details</p>

python python-2.x