how to convert 24 hours to 12 hours in python

Solutions on MaxInterview for how to convert 24 hours to 12 hours in python by the best coders in the world

showing results for - "how to convert 24 hours to 12 hours in python"
Ida
30 Aug 2016
1from datetime import datetime
2d = datetime.strptime("10:30", "%H:%M")
3print(d.strftime("%I:%M %p")) # outputs '10:30 AM'
4d = datetime.strptime("22:30", "%H:%M")
5print(d.strftime("%I:%M %p")) # outputs'10:30 PM'
6