Program to convert time from 12 hour to 24 hour format
Last Updated :
19 Mar, 2025
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 clock
Examples:
Input: 07:05:45 PM
Output: 19:05:45
Explanation: On converting the given time from 12 hour format to 24 hour format we get 19:05:45
Input: 02:15:05 AM
Output: 02:15:05
Explanation: On converting the given time from 12 hour format to 24 hour format we get 02:15:05
Using 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>
Time 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"));
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
How to validate time in 24-hour format using Regular Expression Given a string str, the task is to check whether the string is valid time in 24-hour format or not by using Regular Expression. The valid time in the 24-hour format must satisfy the following conditions. It should start from 0-23 or 00-23.It should be followed by a ':'(colon).It should be followed b
6 min read
How to validate time in 12-hour format using Regular Expression Given a string str, the task is to check whether the string is valid time in 12-hour format or not by using Regular Expression. The valid time in a 12-hour format must satisfy the following conditions:It should start from 1, 2, ... 9 or 10, 11, 12.It should be followed by a colon(:).It should be fol
5 min read
Program to find the time after K minutes from given time You are given a time T in 24-hour format (hh:mm) and a positive integer K, you have to tell the time after K minutes in 24-hour time.Examples: Input: T = 12:43, K = 21 Output: 13:04 Input: T = 20:39, K = 534 Output: 05:33 Approach: Convert the given time in minutesAdd K to it let it be equal to M.Co
6 min read
C++ Program to Print Current Day, Date and Time In order to facilitate finding the current local day, date, and time, C++ has defined several functions in the header file, so functions that will help us in achieving our objective of finding the local day, date, and time are: time(): It is used to find the current calendar time.Its return type is
2 min read
Program to find the time remaining for the day to complete Given the current time in the form of HH::MM, where H represents the hours and M, represents the minutes in a 24-hour time format. The task is to calculate the time remaining for the day to complete as HH::MM.Examples: Input: HH::MM = 00::01 Output: 23::01 Input: HH::MM = 23::55 Output: 00::05 Appro
3 min read