How to convert 24 hour time to 12 hour time in python?

How to convert 24 hour time to 12 hour time in python?

You can convert 24-hour time to 12-hour time format in Python using the datetime module. Here's a step-by-step guide:

from datetime import datetime # Input 24-hour time time_24_hour = "14:30" # Replace with your desired time # Convert to 12-hour time format time_24_obj = datetime.strptime(time_24_hour, "%H:%M") time_12_hour = time_24_obj.strftime("%I:%M %p") print(f"24-hour time: {time_24_hour}") print(f"12-hour time: {time_12_hour}") 

In this example:

  1. We start with the input 24-hour time in the format "HH:MM" (e.g., "14:30").

  2. We use datetime.strptime() to parse the 24-hour time string into a datetime object, specifying the format %H:%M to match the 24-hour time format.

  3. We then use strftime() to convert the datetime object back to a string in 12-hour time format with AM or PM indicator. We specify the format "%I:%M %p" for this purpose.

  4. Finally, we print both the original 24-hour time and the converted 12-hour time.

The output will look like this:

24-hour time: 14:30 12-hour time: 02:30 PM 

You can replace "14:30" with any 24-hour time string, and the code will convert it to the corresponding 12-hour time format.

Examples

  1. How to convert 24-hour time to 12-hour time format in Python?

    • Description: You can use Python's datetime module to convert 24-hour time to 12-hour time format.
    from datetime import datetime # Convert 24-hour time to 12-hour time format time_24hr = "15:30" time_12hr = datetime.strptime(time_24hr, "%H:%M").strftime("%I:%M %p") 
  2. How to change military time to standard time in Python?

    • Description: Use Python's datetime module to convert military time (24-hour time) to standard time (12-hour time format).
    from datetime import datetime # Convert military time to standard time military_time = "17:45" standard_time = datetime.strptime(military_time, "%H:%M").strftime("%I:%M %p") 
  3. How to convert 24-hour format to 12-hour format with AM/PM in Python?

    • Description: Utilize Python's strptime() and strftime() functions from the datetime module to convert 24-hour format to 12-hour format with AM/PM.
    from datetime import datetime # Convert 24-hour format to 12-hour format with AM/PM time_24hr = "18:30" time_12hr = datetime.strptime(time_24hr, "%H:%M").strftime("%I:%M %p") 
  4. How to convert military time to regular time in Python?

    • Description: Convert military time (24-hour format) to regular time (12-hour format) using Python's datetime module.
    from datetime import datetime # Convert military time to regular time military_time = "20:15" regular_time = datetime.strptime(military_time, "%H:%M").strftime("%I:%M %p") 
  5. How to convert 24-hour time to 12-hour time with AM/PM in Python?

    • Description: Use Python's datetime module to convert 24-hour time to 12-hour time format with AM/PM indicator.
    from datetime import datetime # Convert 24-hour time to 12-hour time with AM/PM time_24hr = "22:45" time_12hr = datetime.strptime(time_24hr, "%H:%M").strftime("%I:%M %p") 
  6. How to convert military time to standard time with AM/PM in Python?

    • Description: Employ Python's datetime module to convert military time (24-hour format) to standard time (12-hour format) with AM/PM.
    from datetime import datetime # Convert military time to standard time with AM/PM military_time = "14:30" standard_time = datetime.strptime(military_time, "%H:%M").strftime("%I:%M %p") 
  7. How to convert 24-hour time format to 12-hour time format in Python?

    • Description: Use Python's strptime() and strftime() functions from the datetime module to convert 24-hour time format to 12-hour time format.
    from datetime import datetime # Convert 24-hour time format to 12-hour time format time_24hr = "16:45" time_12hr = datetime.strptime(time_24hr, "%H:%M").strftime("%I:%M %p") 
  8. How to change 24-hour time to 12-hour time with AM/PM in Python?

    • Description: Convert 24-hour time to 12-hour time format with AM/PM using Python's datetime module.
    from datetime import datetime # Change 24-hour time to 12-hour time with AM/PM time_24hr = "19:20" time_12hr = datetime.strptime(time_24hr, "%H:%M").strftime("%I:%M %p") 
  9. How to convert 24-hour clock time to 12-hour clock time in Python?

    • Description: Use Python's datetime module to convert 24-hour clock time to 12-hour clock time with AM/PM.
    from datetime import datetime # Convert 24-hour clock time to 12-hour clock time time_24hr = "21:00" time_12hr = datetime.strptime(time_24hr, "%H:%M").strftime("%I:%M %p") 
  10. How to change military time to regular time with AM/PM in Python?

    • Description: Convert military time (24-hour format) to regular time (12-hour format) with AM/PM using Python's datetime module.
    from datetime import datetime # Change military time to regular time with AM/PM military_time = "23:30" regular_time = datetime.strptime(military_time, "%H:%M").strftime("%I:%M %p") 

More Tags

android-autofill-manager openfiledialog google-apps-script expirationhandler android-uiautomator networkstream mapper io libcurl tail

More Python Questions

More Biology Calculators

More Transportation Calculators

More Various Measurements Units Calculators

More Fitness-Health Calculators