# Program to convert time from 12 hour to 24 hour format

Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.
Note: Midnight is 12:00:00 AM on a 12-hour clock and 00:00:00 on a 24-hour clock. Noon is 12:00:00 PM on 12-hour clock and 12:00:00 on 24-hour clock

Examples:

```Input : A single string containing a time in 12-hour
clock format(hh:mm:ss AM or hh:mm:ss PM
where 01 <= hh <= 12 or 01 <= mm,ss <= 59
Output :Convert and print the given time in 24-hour format,
where 00 <= hh <= 23

Input : 07:05:45PM
Output : 19:05:45
```

## C++

 `// C++ program to convert 12 hour to 24 hour ` `// format ` `#include ` `using` `namespace` `std; ` ` `  `void` `print24(string str) ` `{ ` `    ``// Get hours ` `    ``int` `h1 = (``int``)str - ``'0'``; ` `    ``int` `h2 = (``int``)str - ``'0'``; ` `    ``int` `hh = (h2 * 10 + h1 % 10); ` ` `  `    ``// If time is in "AM" ` `    ``if` `(str == ``'A'``) ` `    ``{ ` `        ``if` `(hh == 12) ` `        ``{ ` `            ``cout << ``"00"``; ` `            ``for` `(``int` `i=2; i <= 7; i++) ` `                ``cout << str[i]; ` `        ``} ` `        ``else` `        ``{ ` `            ``for` `(``int` `i=0; i <= 7; i++) ` `                ``cout << str[i]; ` `        ``} ` `    ``} ` ` `  `    ``// If time is in "PM" ` `    ``else` `    ``{ ` `        ``if` `(hh == 12) ` `        ``{ ` `            ``cout << ``"12"``; ` `            ``for` `(``int` `i=2; i <= 7; i++) ` `                ``cout << str[i]; ` `        ``} ` `        ``else` `        ``{ ` `            ``hh = hh + 12; ` `            ``cout << hh; ` `            ``for` `(``int` `i=2; i <= 7; i++) ` `                ``cout << str[i]; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `   ``string str = ``"07:05:45PM"``; ` `   ``print24(str); ` `   ``return` `0; ` `} `

## Java

 `// Java program to convert 12 hour  ` `// format to  24 hour format ` `class` `GFG ` `{ ` `static` `void` `print24(String str) ` `{ ` `    ``// Get hours ` `    ``int` `h1 = (``int``)str.charAt(``1``) - ``'0'``; ` `    ``int` `h2 = (``int``)str.charAt(``0``) - ``'0'``; ` `    ``int` `hh = (h2 * ``10` `+ h1 % ``10``); ` ` `  `    ``// If time is in "AM" ` `    ``if` `(str.charAt(``8``) == ``'A'``) ` `    ``{ ` `        ``if` `(hh == ``12``) ` `        ``{ ` `            ``System.out.print(``"00"``); ` `            ``for` `(``int` `i = ``2``; i <= ``7``; i++) ` `                ``System.out.print(str.charAt(i)); ` `        ``} ` `        ``else` `        ``{ ` `            ``for` `(``int` `i = ``0``; i <= ``7``; i++) ` `                ``System.out.print(str.charAt(i)); ` `        ``} ` `    ``} ` ` `  `    ``// If time is in "PM" ` `    ``else` `    ``{ ` `        ``if` `(hh == ``12``) ` `        ``{ ` `            ``System.out.print(``"12"``); ` `            ``for` `(``int` `i = ``2``; i <= ``7``; i++) ` `                ``System.out.print(str.charAt(i)); ` `        ``} ` `        ``else` `        ``{ ` `            ``hh = hh + ``12``; ` `            ``System.out.print(hh); ` `            ``for` `(``int` `i = ``2``; i <= ``7``; i++) ` `                ``System.out.print(str.charAt(i)); ` `        ``} ` `    ``} ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main (String[] args) ` `{ ` `    ``String str = ``"07:05:45PM"``; ` `    ``print24(str); ` `} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## C#

 `// C# program to convert 12 hour  ` `// format to 24 hour format ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `static` `void` `print24(String str) ` `{ ` `    ``// Get hours ` `    ``int` `h1 = (``int``)str - ``'0'``; ` `    ``int` `h2 = (``int``)str - ``'0'``; ` `    ``int` `hh = (h2 * 10 + h1 % 10); ` ` `  `    ``// If time is in "AM" ` `    ``if` `(str == ``'A'``) ` `    ``{ ` `        ``if` `(hh == 12) ` `        ``{ ` `            ``Console.Write(``"00"``); ` `            ``for` `(``int` `i = 2; i <= 7; i++) ` `                ``Console.Write(str[i]); ` `        ``} ` `        ``else` `        ``{ ` `            ``for` `(``int` `i = 0; i <= 7; i++) ` `                ``Console.Write(str[i]); ` `        ``} ` `    ``} ` ` `  `    ``// If time is in "PM" ` `    ``else` `    ``{ ` `        ``if` `(hh == 12) ` `        ``{ ` `            ``Console.Write(``"12"``); ` `            ``for` `(``int` `i = 2; i <= 7; i++) ` `                ``Console.Write(str[i]); ` `        ``} ` `        ``else` `        ``{ ` `            ``hh = hh + 12; ` `            ``Console.Write(hh); ` `            ``for` `(``int` `i = 2; i <= 7; i++) ` `                ``Console.Write(str[i]); ` `        ``} ` `    ``} ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``String str = ``"07:05:45PM"``; ` `    ``print24(str); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```19:05:45
```

This article is contributed by Rahul singh (Nit kkr). 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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : Rajput-Ji

Article Tags :
Practice Tags :

5

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