python - Convert the strings to 12 hours format -


i need snippet. have got strings range 0 24.

if have string show 13, convert 12 hours time make string show 1 instead of 13.

for program_end_time in self.program_end_time:     program_stop_hours = str(program_end_time.hour)      #now convert 12 hours instead of 24 

the reason want convert 24 hours 12 hours because want reduce code instead of this:

 if program_stop_hours == "0":      program_stop_hours = "12"      program_am_pm = 'am'  elif program_stop_hours == "1":      program_stop_hours = "1"      program_am_pm = 'am'  elif program_stop_hours == "2":      program_stop_hours = "2"      program_am_pm = 'am'  elif program_stop_hours == "3":      program_stop_hours = "3"      program_am_pm = 'am'  elif program_stop_hours == "4":      program_stop_hours = "4"      program_am_pm = 'am'  elif program_stop_hours == "5":      program_stop_hours = "5"      program_am_pm = 'am'  elif program_stop_hours == "6":      program_stop_hours = "6"      program_am_pm = 'am'  elif program_stop_hours == "7":      program_stop_hours = "7"      program_am_pm = 'am'  elif program_stop_hours == "8":      program_stop_hours = "8"      program_am_pm = 'am'  elif program_stop_hours == "9":      program_stop_hours = "9"      program_am_pm = 'am'  elif program_stop_hours == "10":      program_stop_hours = "10"      program_am_pm = 'am'  elif program_stop_hours == "11":      program_stop_hours = "11"      program_am_pm = 'am'  elif program_stop_hours == "12":      program_stop_hours = "12"      program_am_pm = 'pm'  elif program_stop_hours == "13":      program_stop_hours = "1"      program_am_pm = 'pm'  elif program_stop_hours == "14":      program_stop_hours = "2"      program_am_pm = 'pm'  elif program_stop_hours == "15":      program_stop_hours = "3"      program_am_pm = 'pm'  elif program_stop_hours == "16":      program_stop_hours = "4"      program_am_pm = 'pm'  elif program_stop_hours == "17":      program_stop_hours = "5"      program_am_pm = 'pm'  elif program_stop_hours == "18":      program_stop_hours = "6"      program_am_pm = 'pm'  elif program_stop_hours == "19":      program_stop_hours = "7"      program_am_pm = 'pm'  elif program_stop_hours == "20":      program_stop_hours = "8"      program_am_pm = 'pm'  elif program_stop_hours == "21":      program_stop_hours = "9"      program_am_pm = 'pm'  elif program_stop_hours == "22":      program_stop_hours = "10"      program_am_pm = 'pm'  elif program_stop_hours == "23":      program_stop_hours = "11"      program_am_pm = 'pm' 

i want make show this:

if program_stop_hours >= 0 , if program_stop_hours <= 12:    program_stop_hours = str(program_stop_hours, convert_to_12_hours)    program_am_pm = 'am' else:    program_stop_hours = str(program_stop_hours, convert_to_12_hours)    program_am_pm = 'pm' 

can please show me example how in python?

you need test 2 cases: program_stop_hours less or equal 12 or more 12. can this:

program_stop_hours = input("enter program stop hours: ") if program_stop_hours >= 0 , program_stop_hours <= 12:     program_stop_hours = str(program_stop_hours)     program_am_pm = 'am' else:     program_stop_hours = str(program_stop_hours - 12)     program_am_pm = 'pm'  print program_stop_hours print program_am_pm 

be sure handle various conditions program_stop_hours is: less zero; 12 (make sure intend in case); more 24 (you don't test that); , 24 (to make sure handles case intend).


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -