Hi Friends, today in this tutorial we’ll learn how to solve Time Conversion Hackerrank Solution With Explanation. If you’re wondering how to solve the Time Conversion Hackerrank problem, then you’re at the right place. Here you’ll get all hackerrank solutions with an explanation. But, before getting started we’ll first let you know about hacker rank.

What is Hackerrank?

Hackerrank is a tech company that mainly focuses on competitive programming challenges for students, companies, and consumers. Hackerrank was founded by two founders Vivek Ravishankar and Harishankar Karunanidhi in the year 2012. Its headquarters are situated in California, United States.

With the help of this platform students as well as professionals prepare themselves for the placements by practicing the coding problems. The questions that hacker rank contains are asked in the interview rounds of many companies.

So let us code Time Conversion Hackkerank Solution with Explanation using C, C++, Java, Javascript, and Python.

Time Conversion Hackerrank Solution

Problem Statement:

Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.

Note:

12:00:00 AM on a 12-hour clock is 00:00:00 on a 24-hour clock. 12:00:00 PM on a 12-hour clock is 12:00:00 on a 24 hour clock.

Example:

s = ’12 : 01 : 00 PM’ return ’12 : 01 : 00′.

s = ’12 : 01 : 00 AM’ 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.

the timeConversion has the following parameters:

string s: a time in 12-hour format.

Returns:

string: the time in 24-hour format.

Input Format:

A single string i.e s represents a time in 12-hour clock format (i.e hh:mm:ss AM or hh:mm:ss PM).

Output Format:

Convert the given time into 24-hour clock format and also print it. The time should be 00<=hh<=23.

Constraints:

All types of input times are valid.

Sample Input 0:

07: 05 : 45 PM

Sample Output 0:

19 : 05 : 45

Now we’ll code a time conversion hackerrank solutions using different programming languages.

Time Conversion Hackerrank Solution in C

//Learnprogramo - programming made simple
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
   char t[10];
   scanf("%s", t);
   if(t[8] == 'P') {
     if(t[0] != '1' || t[1] != '2') {
       t[0]++;
       t[1]+=2;
     }
   } else {
     if(t[0] == '1' && t[1] == '2') {
       t[0] = '0';
       t[1] = '0';
     }
   }
   t[8] = '\0';
   printf("%s\n", t);
   return 0;
}

Output:

time conversion hackerrank solution,time conversion hackerrank solution in c,time conversion hackerrank solution in c++,time conversion hackerrank solution in python,time conversion hackerrank solution in c#,time conversion hackerrank solution in python 3,time conversion hackerrank solution in javascript

Explanation:

In the above program, we’ve coded using c language. After compiling the above code the compiler will ask the user to input the time along with AM or PM. Then the compiler will convert entered time into the 24-hour format and print it on the screen.

Time Conversion Hackerrank Solution in C++

//Learnprogramo - programming made simple
#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;
}

Output:

time conversion hackerrank solution,time conversion hackerrank solution in c,time conversion hackerrank solution in c++,time conversion hackerrank solution in python,time conversion hackerrank solution in c#,time conversion hackerrank solution in python 3,time conversion hackerrank solution in javascript

Time Conversion Hackerrank Solution in Java

//Learnprogramo - programming made simple
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);
        }
    }
}

Output:

time conversion hackerrank solution,time conversion hackerrank solution in c,time conversion hackerrank solution in c++,time conversion hackerrank solution in python,time conversion hackerrank solution in c#,time conversion hackerrank solution in python 3,time conversion hackerrank solution in javascript

Time Conversion Solution in Python 3

#Learnprogramo - programming made simple
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')

Output:

time conversion hackerrank solution,time conversion hackerrank solution in c,time conversion hackerrank solution in c++,time conversion hackerrank solution in python,time conversion hackerrank solution in c#,time conversion hackerrank solution in python 3,time conversion hackerrank solution in javascript

Time Conversion Solution in C#

/* Learnprogramo - programming made simple */
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"));
    }
}

Output:

time conversion hackerrank solution,time conversion hackerrank solution in c,time conversion hackerrank solution in c++,time conversion hackerrank solution in python,time conversion hackerrank solution in c#,time conversion hackerrank solution in python 3,time conversion hackerrank solution in javascript

Time Conversion Solution in Javascript

//Learnprogramo - programming made simple
function processData(input) {
    input = input.split(':');
    var hours = parseInt(input[0]);
    var timeFrame = input[2].slice(2);
    var seconds = input[2].slice(0,2);
    if ((timeFrame === 'PM') && (hours !== 12)) {
        hours += 12;
    }
    if ((hours === 12) && (timeFrame === 'AM')) {
        hours = '00';
    } else if (hours < 10) {
        hours = '0' + hours.toString();
    } else {
        hours = hours.toString();
    }
    console.log([hours, input[1], seconds].join(':'));
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});
process.stdin.on("end", function () {
   processData(_input);
});

Output:

time conversion hackerrank solution,time conversion hackerrank solution in c,time conversion hackerrank solution in c++,time conversion hackerrank solution in python,time conversion hackerrank solution in c#,time conversion hackerrank solution in python 3,time conversion hackerrank solution in javascript

Conclusion

So, in this way we’ve learned how to code a time conversion hackerrank solution with an explanation. We’ve coded this problem using different programming languages. If you’ve any doubts then please feel free to contact us. We’ll reach you as soon as possible.

Thanks and Happy Coding :)

Also Read: