Convert to 24 hour time format

”’

Created on Jun 17, 2015

@author: ishaansutaria

”’

def convert24HourFormat(time_str):

    time_str = time_str.upper()

    time_24 =

    if ‘PM’ in time_str and ’12’ in time_str:

        time_24 = time_str

    elif ‘PM’ in time_str:

        temp_list = time_str.split(‘:’)

        time_24 = time_24 + str(int(temp_list[0]) + 12)

    

        time_24 = time_24 + ‘:’ + temp_list[1] + ‘:’ + temp_list[2]

    elif ‘AM’ in time_str and ’12’ in time_str:

        temp_list = time_str.split(‘:’)

        time_24 = time_24 + ‘0’ + str(int(temp_list[0]) – 12)

    

        time_24 = time_24 + ‘:’ + temp_list[1] + ‘:’ + temp_list[2]

    else:

        time_24 = time_str

    

    print time_24[:-2]

    

convert24HourFormat(’12:45:54PM’)       

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s