Program to convert time from 12 hour to 24 hour format Last Updated : 19 Mar, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a time at 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 clockExamples:Input: 07:05:45 PMOutput: 19:05:45Explanation: On converting the given time from 12 hour format to 24 hour format we get 19:05:45Input: 02:15:05 AMOutput: 02:15:05Explanation: On converting the given time from 12 hour format to 24 hour format we get 02:15:05Using Conditional Statements C++ // C++ program to convert 12 hour to 24 hour // format #include<iostream> using namespace std; void print24(string str) { // Get hours int h1 = (int)str[1] - '0'; int h2 = (int)str[0] - '0'; int hh = (h2 * 10 + h1 % 10); // If time is in "AM" if (str[8] == '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 import java.io.*; public 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. Python # Python3 program to convert 12 # hour to 24 hour format def print24(s): # Get hours h1 = ord(s[1]) - ord('0') h2 = ord(s[0]) - ord('0') hh = (h2 * 10 + h1 % 10) # If time is in "AM" if (s[8] == 'A'): if (hh == 12): print('00', end = '') for i in range(2, 8): print(s[i], end = '') else: for i in range(0, 8): print(s[i], end = '') # If time is in "PM" else: if (hh == 12): print("12", end = '') for i in range(2, 8): print(s[i], end = '') else: hh = hh + 12; print(hh, end = '') for i in range(2, 8): print(s[i], end = '') # Driver code if __name__=="__main__": s = "07:05:45PM" print24(s) # This code is contributed by rutvik_56 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[1] - '0'; int h2 = (int)str[0] - '0'; int hh = (h2 * 10 + h1 % 10); // If time is in "AM" if (str[8] == '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 JavaScript <script> // javascript program to convert 12 hour // format to 24 hour format function print24(str) { // Get hours var h1 = Number(str[1] - '0'); var h2 = Number(str[0] - '0'); var hh = (h2 * 10 + h1 % 10); // If time is in "AM" if (str[8] == 'A') { if (hh == 12) { document.write("00"); for (var i = 2; i <= 7; i++) document.write(str[i]); } else { for (var i = 0; i <= 7; i++) document.write(str[i]); } } // If time is in "PM" else { if (hh == 12) { document.write("12"); for (var i = 2; i <= 7; i++) document.write(str[i]); } else { hh = hh + 12; document.write(hh); for (var i = 2; i <= 7; i++) document.write(str[i]); } } } // Driver code var str = "07:05:45PM"; print24(str); // This code is contributed by bunnyram19. </script> Output19:05:45Time Complexity: O(1)Auxiliary Space: O(1)Using Library Methods C++ #include <iostream> #include <ctime> using namespace std; string englishTime(string input) { // Format of the date defined in the input String struct tm tm; strptime(input.c_str(), "%I:%M:%S %p", &tm); // Changing the format of date and storing it in string char output[9]; strftime(output, sizeof(output), "%H:%M:%S", &tm); return string(output); } // Driver code int main() { cout << englishTime("07:05:45 PM") << endl; return 0; } Java // Java program for the above approach import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class GFG { public static String englishTime(String input) throws ParseException { // Format of the date defined in the input String DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss aa"); // Change the pattern into 24 hour format DateFormat format = new SimpleDateFormat("HH:mm:ss"); Date time = null; String output = ""; // Converting the input String to Date time = dateFormat.parse(input); // Changing the format of date // and storing it in // String output = format.format(time); return output; } // Driver Code public static void main(String[] arg) throws ParseException { System.out.println(englishTime("07:05:45 PM")); } } Python # Python program for the above approach import datetime def englishTime(input): # Format of the date defined in the input String dateFormat = datetime.datetime.strptime(input, "%I:%M:%S %p") # Change the pattern into 24 hour format format = datetime.datetime.strftime(dateFormat, "%H:%M:%S") return format # Driver Code print(englishTime("07:05:45 PM")) C# using System; using System.Globalization; class GFG { public static string englishTime(string input) { // Format of the date defined in the input String DateTime time; CultureInfo provider = CultureInfo.InvariantCulture; DateTime.TryParseExact(input, "hh:mm:ss tt", provider, DateTimeStyles.None, out time); // Change the pattern into 24 hour format DateTimeFormatInfo dtfi = CultureInfo.InvariantCulture.DateTimeFormat; string output = time.ToString("HH:mm:ss", dtfi); return output; } // Driver Code static void Main(string[] args) { Console.WriteLine(englishTime("07:05:45 PM")); } } JavaScript // Function to convert 12 hour time format to 24 hour time format function englishTime(input) { // Create a Date object with the input time string let date = new Date("1970-01-01 " + input); // Format the date object into a 24 hour time string let format = date.toLocaleTimeString([], { hour12: false }); return format; } // Driver Code console.log(englishTime("07:05:45 PM")); Output19:05:45Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to convert time from 12 hour to 24 hour format K kartik Improve Article Tags : DSA Basic Coding Problems date-time-program Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous 4 min read Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ 3 min read Like