HackerRank Time Conversion Problem Solution Yashwant Parihar, April 11, 2023April 11, 2023 In this post, We are going to solve HackerRankTime Conversion 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 time conversion function in the editor below. It should return a new string representing the input time in 24-hour format. time conversion has the following parameter(s): string s: a time in 12-hour format Returns string: the time in 24-hour format Constraints All input times are valid Sample Input 0 07:05:45PM Sample Output 0 19:05:45 HackerRank Time Conversion Problem Solution Time Conversion C Solution #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { char input[15]; int hours; int minutes; int seconds; int side; scanf("%s", input); if (input[8] == 'A'){ side = 0; } else{ side = 1; } hours = atoi(strtok(input, ":")); minutes = atoi(strtok(NULL, ":")); seconds = atoi(strtok(NULL, "AP")); if (side == 1){ if (hours != 12){ hours += 12; } } else if (hours == 12){ hours = 0; } if (hours < 10){ printf("0%d", hours); } else{ printf("%d", hours); } printf(":"); if (minutes < 10){ printf("0%d", minutes); } else{ printf("%d", minutes); } printf(":"); if (seconds < 10){ printf("0%d", seconds); } else{ printf("%d", seconds); } return 0; } Time Conversion C++ Solution #include <iostream> #include <algorithm> using namespace std; int main() { string time; cin >> time; std::transform(time.begin(), time.end(), time.begin(), ::toupper); if (time.find("AM") != string::npos) { time.erase(time.end() - 2, time.end()); int hour = stoi(time.substr(0, 2)); if (hour == 12) { time.replace(0, 2, "00"); } } else if (time.find("PM") != string::npos) { time.erase(time.end() - 2, time.end()); int hour = stoi(time.substr(0, 2)); if (hour != 12) { hour = hour + 12; } time.replace(0, 2, to_string(hour)); } else { cout << "Wrong 12H format\n"; return 0; } cout << time << '\n'; return 0; } Time Conversion C Sharp Solution 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 */ String s = Console.ReadLine(); int hours = Int32.Parse(s.Substring(0,2)); int mins = Int32.Parse(s.Substring(3,2)); int secs = Int32.Parse(s.Substring(6,2)); String half = s.Substring(8,2); if(half.CompareTo("AM")==0){ if(hours==12) hours=0; }else{ if(hours!=12) hours+=12; } String h = hours.ToString(); String m = mins.ToString(); String sc = secs.ToString(); if(hours<10) h = "0"+h; if(mins<10) m = "0"+m; if(secs<10) sc = "0"+sc; Console.WriteLine(h+":"+m+":"+sc); //Console.WriteLine(mins); } } Time Conversion Java Solution import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'timeConversion' function below. * * The function is expected to return a STRING. * The function accepts STRING s as parameter. */ static String timeConversion(String timeString) { if (timeString.charAt(8) == 'P') { String portion = timeString.substring(0, 8); String[] times = portion.split(":"); if (times[0].matches("12")) { return times[0] + ":" + times[1] + ":" + times[2]; } times[0] = String.valueOf(Integer.valueOf(times[0]) + 12); return times[0] + ":" + times[1] + ":" + times[2]; } else { String portion = timeString.substring(0, 8); String[] times = portion.split(":"); if (times[0].matches("12")) { times[0] = "00"; return times[0] + ":" + times[1] + ":" + times[2]; } return timeString.substring(0, 8); } } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); String s = bufferedReader.readLine(); String result = Result.timeConversion(s); bufferedWriter.write(result); bufferedWriter.newLine(); bufferedReader.close(); bufferedWriter.close(); } } Time Conversion JavaScript Solution # Enter your code here. Read input from STDIN. Print output to STDOUT import re d = raw_input() d_list = d.strip().split(":") hh = int(d_list[0]) mm = int(d_list[1]) ap = re.findall(r'[A-Z]+',d_list[2])[0] ss = int(re.findall(r'[0-9]+',d_list[2])[0]) hhh = hh+12 if ap == 'AM' and hh <= 11: print ("%02d" %hh)+':'+("%02d" %mm)+':'+("%02d" %ss) if ap =="AM" and hh == 12: print ("%02d" %0)+':'+("%02d" %mm)+':'+("%02d" %ss) if ap == 'PM' and hh <= 11: print ("%02d" %hhh)+':'+("%02d" %mm)+':'+("%02d" %ss) if ap == 'PM' and hh == 12: print ("%02d" %12)+':'+("%02d" %mm)+':'+("%02d" %ss) Time Conversion Python Solution i_time = input() if i_time == '12:00:00AM': print('00:00:00') exit() if i_time == '12:00:00PM': print('12:00:00') exit() if i_time[8] == 'A': hour = int(i_time[:2]) if hour == 12: hour = 0 hour = "{:02.0f}".format(hour) print(str(hour)+i_time[2:8]) else: hour = int(i_time[:2])+12 if hour == 24: hour = 12 hour = "{:02.0f}".format(hour) print(str(hour)+i_time[2:8]) Other solutions HackerRank Grading Students Problem Solution HackerRank Apple and Orange Problem Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython