Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.
Note: – 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
– 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.
Example
- s = ‘12:01:00PM’
Return ’12:01:00′.
- s = ‘12:01:00PM’
Return ’00:01:00′.
Function Description
Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.
timeConversion has the following parameter(s):
- string s: a time in 12 hour format
Returns
- string: the time in 24 hour format
Input Format
A single string s that represents a time in 12-hour clock format (i.e.:hh:mm:ssAM or hh:mm:ssPM).
Constraints
- All input times are valid
Sample Input 0
07:05:45PM
Sample Output 0
19:05:45
Time Conversion HackerRank Solution in C
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    char timestamp[11] = "\0\0\0\0\0\0\0\0\0\0\0";
    int hr=0;
    
    scanf("%s", timestamp);
    
    if(timestamp[8] == 'P'){
        hr = 10*(timestamp[0]-'0')+(timestamp[1]-'0');
        if(hr < 12) hr += 12;
    }
    else{
        hr = 10*(timestamp[0]-'0')+(timestamp[1]-'0');
        if(hr == 12) hr = 0;
    }
    
    timestamp[0] = hr/10 + '0';
    timestamp[1] = hr%10 + '0';
    timestamp[8] = '\0';
    timestamp[9] = '\0';
    
    printf("%s", timestamp);
    return 0;
}
Time Conversion HackerRank Solution in C++
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    string s;
    string h;
    int hr;
    cin>>s;
    hr = ((s[0]-'0')*10)+(s[1]-'0');
    if(s[8]=='P'&&s[9]=='M'&& hr ==12) cout<<to_string(hr);
    else if(s[8]=='P'&&s[9]=='M') cout<<to_string(hr+12);
    else if(s[8]=='A'&&s[9]=='M'&&hr==12) cout<<"00";
    
    else cout<< s[0]<<s[1];
    
   
    for(int i =2;i<8;i++)
        cout<<s[i];
    cout<<endl;
    return 0;
}
Time Conversion HackerRank Solution in Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
   	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine(); //07:05:45PM
        DateFormat inFormat = new SimpleDateFormat( "hh:mm:ssaa");
        DateFormat outFormat = new SimpleDateFormat( "HH:mm:ss");
        Date date = null;
        try {
            date = inFormat.parse(s); 
        }catch (ParseException e ){
            e.printStackTrace();
        }
        if( date != null ){
            String myDate = outFormat.format(date);
            System.out.println(myDate);
        }
    }
}
Time Conversion HackerRank Solution in Python
import re
time = raw_input()
times = re.search(r"(?P<hh>..):(?P<mm>..):(?P<ss>..)(?P<ampm>..)", time)
hour = times.group('hh')
if times.group('ampm') == 'PM':
    if hour == '12':
        hr = '12'
    else:
        hr = str(int(hour) + 12)
else:
    if hour == '12':
        hr = '00'
    else:
        hr = hour
print hr + ':' + times.group('mm') + ':' + times.group('ss')
Time Conversion HackerRank Solution in C#
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        Console.WriteLine(DateTime.Parse(Console.ReadLine()).ToString("HH:mm:ss"));
    }
}
Attempt – Time Conversion HackerRank Challenge
Link: https://www.hackerrank.com/challenges/time-conversion
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.
public static String timeConversion(String s) {
String ampm = s.substring(s.length()-2, s.length()-1);
StringBuffer sb = new StringBuffer(s.substring(0, s.length()-2));
int start = Integer.parseInt(s.substring(0, 2));
if(s.contains(“P”)){
String sb1 = start == 12 ? “12”: “”+(start+12);
sb.replace(0, 2, “” +sb1);
System.out.println(sb);
} else {
String sb1 = start == 12 ? “00”: start > 9? “”+start: “0” +start;
sb.replace(0, 2, “” +sb1);
System.out.println(sb);
}
return sb.toString();
}
}