Given a time in a 24-hour clock (military time), convert it to 12-hour clock format.
Note: Midnight is 00:00:00 on a 24-hour clock and 12:00:00 AM on a 12-hour clock. Noon is 12:00:00 on 24-hour clock and 12:00:00 PM on 12-hour clock.
A string will be given of the format hh:mm:ss and output should be in the format hh:mm:ss AM or hh:mm:ss PM in 12hour clock. Here hh represents hour, mm represents minutes and ss represents seconds.
Input : 17:35:20 Output : 5:35:20 PM Input : 00:10:24 Output : 12:10:24 AM
The approach to solving this problem requires some observations. First that the minutes and seconds will be same in both the cases. The only change will be in the hours and according to that Meridien will be appended to the string.
For hours, first convert it from string to int datatype, then take its modulo with 12 and that will be our hours in 12-hour format. Still, there will be a case when hour becomes 00 i.e (12 or 00 in 24-hour format) which we need to handle separately.
Below is the implementation of above approach:
- Python program to convert time from 12 hour to 24 hour format
- C++ program to convert time from 12 hour to 24 hour format
- Changing One Clock Time to Other Time in Minimum Number of Operations
- Find time when hour and minute hands superimpose
- Find a time for which angle between hour and minute hands is given theta
- C program to print digital clock with current time
- Time when minute hand and hour hand coincide
- Count how many times the given digital clock shows identical digits
- Program to Convert Milliseconds to a Date Format in Java
- Perform n steps to convert every digit of a number in the format [count][digit]
- How to convert timestamp to time ago in PHP ?
- Convert given time into words
- Convert timestamp to readable date/time in PHP
- Time difference between expected time and given time
- Program to find the time after K minutes from given time
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.