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 : 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++
#include<iostream>
using namespace std;
void print24(string str)
{
int h1 = ( int )str[1] - '0' ;
int h2 = ( int )str[0] - '0' ;
int hh = (h2 * 10 + h1 % 10);
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];
}
}
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];
}
}
}
int main()
{
string str = "07:05:45PM" ;
print24(str);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static void print24(String str)
{
int h1 = ( int )str.charAt( 1 ) - '0' ;
int h2 = ( int )str.charAt( 0 ) - '0' ;
int hh = (h2 * 10 + h1 % 10 );
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));
}
}
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));
}
}
}
public static void main (String[] args)
{
String str = "07:05:45PM" ;
print24(str);
}
}
|
Python3
def print24(s):
h1 = ord (s[ 1 ]) - ord ( '0' )
h2 = ord (s[ 0 ]) - ord ( '0' )
hh = (h2 * 10 + h1 % 10 )
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 = '')
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 = '')
if __name__ = = "__main__" :
s = "07:05:45PM"
print24(s)
|
C#
using System;
class GFG
{
static void print24(String str)
{
int h1 = ( int )str[1] - '0' ;
int h2 = ( int )str[0] - '0' ;
int hh = (h2 * 10 + h1 % 10);
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]);
}
}
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]);
}
}
}
public static void Main(String[] args)
{
String str = "07:05:45PM" ;
print24(str);
}
}
|
Javascript
<script>
function print24(str)
{
var h1 = Number(str[1] - '0' );
var h2 = Number(str[0] - '0' );
var hh = (h2 * 10 + h1 % 10);
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]);
}
}
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]);
}
}
}
var str = "07:05:45PM" ;
print24(str);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 2: Using java Date
C++
#include <iostream>
#include <ctime>
using namespace std;
string englishTime(string input)
{
struct tm tm ;
strptime(input.c_str(), "%I:%M:%S %p" , & tm );
char output[9];
strftime (output, sizeof (output), "%H:%M:%S" , & tm );
return string(output);
}
int main() {
cout << englishTime( "07:05:45 PM" ) << endl;
return 0;
}
|
Java
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
{
DateFormat dateFormat
= new SimpleDateFormat( "hh:mm:ss aa" );
DateFormat format
= new SimpleDateFormat( "HH:mm:ss" );
Date time = null ;
String output = "" ;
time = dateFormat.parse(input);
output = format.format(time);
return output;
}
public static void main(String[] arg)
throws ParseException
{
System.out.println(englishTime( "07:05:45 PM" ));
}
}
|
Python3
import datetime
def englishTime( input ):
dateFormat = datetime.datetime.strptime( input , "%I:%M:%S %p" )
format = datetime.datetime.strftime(dateFormat, "%H:%M:%S" )
return format
print (englishTime( "07:05:45 PM" ))
|
C#
using System;
using System.Globalization;
class GFG {
public static string englishTime( string input)
{
DateTime time;
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime.TryParseExact(input, "hh:mm:ss tt" , provider, DateTimeStyles.None, out time);
DateTimeFormatInfo dtfi = CultureInfo.InvariantCulture.DateTimeFormat;
string output = time.ToString( "HH:mm:ss" , dtfi);
return output;
}
static void Main( string [] args) {
Console.WriteLine(englishTime( "07:05:45 PM" ));
}
}
|
Javascript
function englishTime(input) {
let date = new Date( "1970-01-01 " + input);
let format = date.toLocaleTimeString([], { hour12: false });
return format;
}
console.log(englishTime( "07:05:45 PM" ));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
This article is contributed by Rahul singh (Nit kkr). If you like neveropen and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!