# Convert time from 24 hour clock to 12 hour clock format

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.

Examples:

```Input : 17:35:20
Output : 5:35:20 PM

Input : 00:10:24
Output : 12:10:24 AM
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## C++

 `// CPP program to convert time from 24 hour ` `// to 12 hour format ` ` `  `#include ` ` `  `using` `namespace` `std; ` ` `  `// Convert Function which takes in ` `// 24hour time and convert it to ` `// 12 hour format ` `void` `convert12(string str) ` `{ ` `    ``// Get Hours ` `    ``int` `h1 = (``int``)str - ``'0'``; ` `    ``int` `h2 = (``int``)str - ``'0'``; ` ` `  `    ``int` `hh = h1 * 10 + h2; ` ` `  `    ``// Finding out the Meridien of time ` `    ``// ie. AM or PM ` `    ``string Meridien; ` `    ``if` `(hh < 12) { ` `        ``Meridien = ``"AM"``; ` `    ``} ` `    ``else` `        ``Meridien = ``"PM"``; ` ` `  `    ``hh %= 12; ` ` `  `    ``// Handle 00 and 12 case separately ` `    ``if` `(hh == 0) { ` `        ``cout << ``"12"``; ` ` `  `        ``// Printing minutes and seconds ` `        ``for` `(``int` `i = 2; i < 8; ++i) { ` `            ``cout << str[i]; ` `        ``} ` `    ``} ` `    ``else` `{ ` `        ``cout << hh; ` `        ``// Printing minutes and seconds ` `        ``for` `(``int` `i = 2; i < 8; ++i) { ` `            ``cout << str[i]; ` `        ``} ` `    ``} ` ` `  `    ``// After time is printed ` `    ``// cout Meridien ` `    ``cout << ``" "` `<< Meridien << ``'\n'``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// 24 hour format ` `    ``string str = ``"17:35:20"``; ` ` `  `    ``convert12(str); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to convert time from 24 hour ` `// to 12 hour format ` ` `  `import` `java.util.*; ` `// Convert Function which takes in ` `// 24hour time and convert it to ` `// 12 hour format ` `class` `GFG ` `{ ` ` `  `static` `void` `convert12(String str) ` `{ ` `// Get Hours ` `    ``int` `h1 = (``int``)str.charAt(``0``) - ``'0'``; ` `    ``int` `h2 = (``int``)str.charAt(``1``)- ``'0'``; ` ` `  `    ``int` `hh = h1 * ``10` `+ h2; ` ` `  `    ``// Finding out the Meridien of time ` `    ``// ie. AM or PM ` `    ``String Meridien; ` `    ``if` `(hh < ``12``) { ` `        ``Meridien = ``"AM"``; ` `    ``} ` `    ``else` `        ``Meridien = ``"PM"``; ` ` `  `    ``hh %= ``12``; ` ` `  `    ``// Handle 00 and 12 case separately ` `    ``if` `(hh == ``0``) { ` `        ``System.out.print(``"12"``); ` ` `  `        ``// Printing minutes and seconds ` `        ``for` `(``int` `i = ``2``; i < ``8``; ++i) { ` `        ``System.out.print(str.charAt(i)); ` `        ``} ` `    ``} ` `    ``else` `{ ` `        ``System.out.print(hh); ` `        ``// Printing minutes and seconds ` `        ``for` `(``int` `i = ``2``; i < ``8``; ++i) { ` `        ``System.out.print(str.charAt(i)); ` `        ``} ` `    ``} ` ` `  `    ``// After time is printed ` `    ``// cout Meridien ` `System.out.println(``" "``+Meridien); ` `} ` ` `  `//Driver code ` `public` `static` `void` `main(String ar[]) ` `{ ` ` `  `// 24 hour format ` `    ``String str = ``"17:35:20"``; ` `    ``convert12(str); ` ` `  `} ` `} `

## Python3

 `# Python program to convert time from 24 hour ` `# to 12 hour format ` ` `  `# Convert Function which takes in ` `# 24hour time and convert it to ` `# 12 hour format ` `def` `convert12(``str``): ` ` `  `    ``# Get Hours ` `    ``h1 ``=` `ord``(``str``[``0``]) ``-` `ord``(``'0'``); ` `    ``h2 ``=` `ord``(``str``[``1``]) ``-` `ord``(``'0'``); ` ` `  `    ``hh ``=` `h1 ``*` `10` `+` `h2; ` ` `  `    ``# Finding out the Meridien of time ` `    ``# ie. AM or PM ` `    ``Meridien``=``""; ` `    ``if` `(hh < ``12``): ` `        ``Meridien ``=` `"AM"``; ` `    ``else``: ` `        ``Meridien ``=` `"PM"``; ` ` `  `    ``hh ``%``=` `12``; ` ` `  `    ``# Handle 00 and 12 case separately ` `    ``if` `(hh ``=``=` `0``): ` `        ``print``(``"12"``, end ``=` `""); ` ` `  `        ``# Printing minutes and seconds ` `        ``for` `i ``in` `range``(``2``, ``8``): ` `            ``print``(``str``[i], end ``=` `""); ` ` `  `    ``else``: ` `        ``print``(hh,end``=``""); ` `         `  `        ``# Printing minutes and seconds ` `        ``for` `i ``in` `range``(``2``, ``8``): ` `            ``print``(``str``[i], end ``=` `""); ` ` `  `    ``# After time is printed ` `    ``# cout Meridien ` `    ``print``(``" "` `+` `Meridien); ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` ` `  `    ``# 24 hour format ` `    ``str` `=` `"17:35:20"``; ` `    ``convert12(``str``); ` ` `  `# This code is contributed by 29AjayKumar `

## C#

 `// C# program to convert time from 24 hour ` `// to 12 hour format ` `  `  `using` `System; ` `// Convert Function which takes in ` `// 24hour time and convert it to ` `// 12 hour format ` `class` `GFG ` `{ ` `  `  `static` `void` `convert12(``string` `str) ` `{ ` `// Get Hours ` `    ``int` `h1 = (``int``)str - ``'0'``; ` `    ``int` `h2 = (``int``)str- ``'0'``; ` `  `  `    ``int` `hh = h1 * 10 + h2; ` `  `  `    ``// Finding out the Meridien of time ` `    ``// ie. AM or PM ` `    ``string` `Meridien; ` `    ``if` `(hh < 12) { ` `        ``Meridien = ``"AM"``; ` `    ``} ` `    ``else` `        ``Meridien = ``"PM"``; ` `  `  `    ``hh %= 12; ` `  `  `    ``// Handle 00 and 12 case separately ` `    ``if` `(hh == 0) { ` `        ``Console.Write(``"12"``); ` `  `  `        ``// Printing minutes and seconds ` `        ``for` `(``int` `i = 2; i < 8; ++i) { ` `        ``Console.Write(str[i]); ` `        ``} ` `    ``} ` `    ``else` `{ ` `        ``Console.Write(hh); ` `        ``// Printing minutes and seconds ` `        ``for` `(``int` `i = 2; i < 8; ++i) { ` `        ``Console.Write(str[i]); ` `        ``} ` `    ``} ` `  `  `    ``// After time is printed ` `    ``// cout Meridien ` `Console.WriteLine(``" "``+Meridien); ` `} ` `  `  `//Driver code ` `public` `static` `void` `Main() ` `{ ` `  `  `// 24 hour format ` `    ``string` `str = ``"17:35:20"``; ` `    ``convert12(str); ` `  `  `} ` `} `

## PHP

 ` `

Output:

```5:35:20 PM
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.