Hello Programmers, In this post, you will know how to solve the HackerRank Time Conversion Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank Time Conversion Solution
Problem
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:00AM’ 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
HackerRank Time Conversion Solutions
Time Conversion 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 Solution in Cpp
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 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 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 Solution in Python
# Enter your code here. Read input from STDIN. Print output to STDOUT date = raw_input() d = date.split(':') #12:00:00AM hour = int(d[0]) minute = int(d[1]) second = int(d[2][0:2]) am_pm = d[2][2:4] if 'AM' == am_pm: if 12 == hour: print '00:'+d[1]+':'+d[2][0:2] else: print date[0:8] else: if 12 == hour: print '12:'+d[1]+':'+d[2][0:2] else: hour = hour + 12 print str(hour) + date[2:8]
Time Conversion Solution using JavaScript
function processData(input) { //Enter your code here var amPm = input.substring(input.length-2); if(amPm == 'PM'){ var replace=parseInt(input.substring(0,2)) input = input.replace(input.substring(0,2),(replace < 12 ) ? replace+12:replace); input = input.substring(0, input.length-2); }else{ if(parseInt(input.substring(0, 2)) == 12){ input = input.replace(input.substring(0,2), '00'); input = input.substring(0, input.length-2); }else{ input = input.substring(0, input.length-2); } } console.log(input) } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });
Time Conversion Solution in Scala
object Solution { def main(args: Array[String]) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ val s = readLine var hour = s.substring(0, 2).toInt val remain = s.drop(2).dropRight(2) if (s.endsWith("PM")) { if (hour != 12) { hour = hour + 12 } println(hour + remain) } else { hour = hour % 12 if (hour < 10) { print("0" + hour + remain) } else { print(hour + remain) } } } }
Time Conversion Solution in Pascal
var s,h,m,se,t:string; hs,ms,ss:longint; code:integer; begin readln(s); h:=copy(s,1,2); m:=copy(s,4,2); se:=copy(s,7,2); t:=copy(s,9,2); if (t='AM') and (h<>'12') then write(h,':',m,':',se) else if (t='PM') and (h<>'12') then begin if h[1]='0' then delete(h,1,1); val(h,hs,code); hs:=hs+12; if hs=24 then hs:=0; str(hs,h); write(h,':',m,':',se); end else if (h='12') and (t='AM') then write('00:',m,':',se) else write(h,':',m,':',se); end.
Disclaimer: This problem (Time Conversion) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.