Given a time in a 24-hour clock (military time), convert it to a 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 a 12hour clock. Here hh represents an 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
The approach to solving this problem requires some observations. First that the minutes and seconds will be the same in both 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 the hour becomes 00 i.e (12 or 00 in 24-hour format) which we need to handle separately.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void convert12(string str)
{
int h1 = ( int )str[0] - '0' ;
int h2 = ( int )str[1] - '0' ;
int hh = h1 * 10 + h2;
string Meridien;
if (hh < 12) {
Meridien = "AM" ;
}
else
Meridien = "PM" ;
hh %= 12;
if (hh == 0) {
cout << "12" ;
for ( int i = 2; i < 8; ++i) {
cout << str[i];
}
}
else {
cout << hh;
for ( int i = 2; i < 8; ++i) {
cout << str[i];
}
}
cout << " " << Meridien << '\n' ;
}
int main()
{
string str = "17:35:20" ;
convert12(str);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void convert12(String str)
{
int h1 = ( int )str.charAt( 0 ) - '0' ;
int h2 = ( int )str.charAt( 1 )- '0' ;
int hh = h1 * 10 + h2;
String Meridien;
if (hh < 12 ) {
Meridien = "AM" ;
}
else
Meridien = "PM" ;
hh %= 12 ;
if (hh == 0 ) {
System.out.print( "12" );
for ( int i = 2 ; i < 8 ; ++i) {
System.out.print(str.charAt(i));
}
}
else {
System.out.print(hh);
for ( int i = 2 ; i < 8 ; ++i) {
System.out.print(str.charAt(i));
}
}
System.out.println( " " +Meridien);
}
public static void main(String ar[])
{
String str = "17:35:20" ;
convert12(str);
}
}
|
Python3
def convert12( str ):
h1 = ord ( str [ 0 ]) - ord ( '0' );
h2 = ord ( str [ 1 ]) - ord ( '0' );
hh = h1 * 10 + h2;
Meridien = "";
if (hh < 12 ):
Meridien = "AM" ;
else :
Meridien = "PM" ;
hh % = 12 ;
if (hh = = 0 ):
print ( "12" , end = "");
for i in range ( 2 , 8 ):
print ( str [i], end = "");
else :
print (hh,end = "");
for i in range ( 2 , 8 ):
print ( str [i], end = "");
print ( " " + Meridien);
if __name__ = = '__main__' :
str = "17:35:20" ;
convert12( str );
|
C#
using System;
class GFG
{
static void convert12( string str)
{
int h1 = ( int )str[0] - '0' ;
int h2 = ( int )str[1]- '0' ;
int hh = h1 * 10 + h2;
string Meridien;
if (hh < 12) {
Meridien = "AM" ;
}
else
Meridien = "PM" ;
hh %= 12;
if (hh == 0) {
Console.Write( "12" );
for ( int i = 2; i < 8; ++i) {
Console.Write(str[i]);
}
}
else {
Console.Write(hh);
for ( int i = 2; i < 8; ++i) {
Console.Write(str[i]);
}
}
Console.WriteLine( " " +Meridien);
}
public static void Main()
{
string str = "17:35:20" ;
convert12(str);
}
}
|
PHP
<?php
function convert12( $str )
{
$h1 = $str [0] - '0' ;
$h2 = $str [1] - '0' ;
$hh = $h1 * 10 + $h2 ;
$Meridien ;
if ( $hh < 12)
{
$Meridien = "AM" ;
}
else
$Meridien = "PM" ;
$hh %= 12;
if ( $hh == 0)
{
echo "12" ;
for ( $i = 2; $i < 8; ++ $i )
{
echo $str [ $i ];
}
}
else
{
echo $hh ;
for ( $i = 2; $i < 8; ++ $i )
{
echo $str [ $i ];
}
}
echo " " , $Meridien ;
}
$str = "17:35:20" ;
convert12( $str );
?>
|
Javascript
<script>
function convert12(str)
{
var h1 = Number(str[0] - '0' );
var h2 = Number(str[1] - '0' );
var hh = h1 * 10 + h2;
var Meridien;
if (hh < 12) {
Meridien = "AM" ;
}
else
Meridien = "PM" ;
hh %= 12;
if (hh == 0) {
document.write( "12" );
for ( var i = 2; i < 8; ++i) {
document.write(str[i]);
}
}
else {
document.write(hh);
for ( var i = 2; i < 8; ++i) {
document.write(str[i]);
}
}
document.write( " " +Meridien);
}
var str = "17:35:20" ;
convert12(str);
</script>
|
Time Complexity: O(8) = O(1)
Auxiliary Space: O(1)