55

I have the following 24-hour times:

{'Wed': '10:30 - 21:00', 'Sun': '10:30 - 21:00', 'Thu': '10:30 - 21:00', 
 'Mon': '10:30 - 21:00', 'Fri': '10:30 - 22:00', 'Tue': '10:30 - 21:00', 
 'Sat': '10:30 - 22:00'}

How can I convert this to 12-hour time?

{'Wed': '10:30 AM - 09:00 PM', 'Sun': '10:30 AM - 09:00 PM', 
 'Thu': '10:30 AM - 09:00 PM', 'Mon': '10:30 AM - 09:00 PM', 
 'Fri': '10:30 AM- 10:00 PM', 'Tue': '10:30 AM- 09:00 PM', 
 'Sat': '10:30 AM - 10:00 PM'}

I want to intelligently convert "10.30" to "10.30 AM" & "22:30" to "10:30 PM". I can do using my own logic but is there a way to do this intelligently without if... elif?

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
Garfield
  • 2,487
  • 4
  • 31
  • 54
  • 1
    Similar but the other way round http://stackoverflow.com/questions/440061/convert-12-hour-date-time-to-24-hour-date-time – Trufa Dec 13 '12 at 07:45

7 Answers7

119
>>> from datetime import datetime
>>> d = datetime.strptime("10:30", "%H:%M")
>>> d.strftime("%I:%M %p")
'10:30 AM'
>>> d = datetime.strptime("22:30", "%H:%M")
>>> d.strftime("%I:%M %p")
'10:30 PM'
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
17

The key to this code is to use the library function time.strptime() to parse the 24-hour string representations into a time.struct_time object, then use library function time.strftime() to format this struct_time into a string of your desired 12-hour format.

I'll assume you have no trouble writing a loop, to iterate through the values in the dict and to break the string into two substrings with one time value each.

For each substring, convert the time value with code like:

import time
t = time.strptime(timevalue_24hour, "%H:%M")
timevalue_12hour = time.strftime( "%I:%M %p", t )

The question, Converting string into datetime, also has helpful answers.

Community
  • 1
  • 1
Jim DeLaHunt
  • 10,960
  • 3
  • 45
  • 74
8

I know many people answer this in a similar way but the more easier way is

import time
t = time.strftime("%I:%M %p")
print(t)

the %I gives the 12 hour clock hour and %M minute, %p returns the PM or AM value. In my case it returned

05:27 PM
random_hooman
  • 1,660
  • 7
  • 23
7
>>> from datetime import datetime
>>> s = datetime.strptime("13:30", "%H:%M")
>>> print(s.strftime("%r"))
01:30:00 PM
Satyam Anand
  • 437
  • 4
  • 11
3
import time

# get current time
date_time = time.strftime("%b %d %Y %-I:%M %p")

the above outputs: May 27 2020 7:26 PM ...at least for me right now ;)

Laurence
  • 409
  • 3
  • 7
  • 17
2

Python's strftime use %I

reference http://strftime.org/

javis
  • 27
  • 3
0

One may ask, why add an answer if there are already relevant ones?
Well, I realized that some questions have been closed without actually getting an accurate solution so I chose to add this solution for OPs who are looking to accomplish same results using only logic and avoiding any import. such as This OP

def convert_time(time_string):
    hours, minutes = map(int, time_string.split(":"))
    meridian = "AM"
    if hours > 12:
        hours -= 12
        meridian = "PM"
    elif hours == 12:
        meridian = "PM"
    elif hours == 0:
        hours = 12
    return f"{hours:02}:{minutes:02} {meridian}"
# Call the function:
time_dict = {'Wed': '10:30 - 21:00', 'Sun': '10:30 - 21:00', 'Thu': '10:30 - 21:00', 
 'Mon': '10:30 - 21:00', 'Fri': '10:30 - 22:00', 'Tue': '10:30 - 21:00', 
 'Sat': '10:30 - 22:00'}

for day, hours in time_dict.items():
    start_time, end_time = map(lambda x: x.strip(), hours.split("-"))
    time_dict[day] = f"{convert_time(start_time)} - {convert_time(end_time)}"

print(time_dict)

{'Wed': '10:30 AM - 09:00 PM', 'Sun': '10:30 AM - 09:00 PM', 
'Thu': '10:30 AM - 09:00 PM', 'Mon': '10:30 AM - 09:00 PM', 
'Fri': '10:30 AM - 10:00 PM', 'Tue': '10:30 AM - 09:00 PM', 
'Sat': '10:30 AM - 10:00 PM'}

If your time input is a string like "00:29", just go ahead and call the function:

str_time_input = "00:29"
print(convert_time(str_time_input)) # 12:29 AM

What if you have both hours and minutes as int?:

hours = 12
minutes = 17
str_time = f"{hours}:{minutes}"
print(type(str_time))
print(convert_time(str_time))

<class 'str'>
12:17 PM
Jamiu S.
  • 5,257
  • 5
  • 12
  • 34