I am developing a Console
application for converting the time from 12 hours format to 24 hours format:
input: 02:03:34PM expected output:14:03:34
But I am getting 14:3:34 Below is my code snippet:
string[] arr_temp = Console.ReadLine().Split(':');
string time = arr_temp[2].ToUpper().Contains("AM") ? "AM" : "PM";
string sec=string.Empty;
for (int i = 0; i < 2; i++)
{
sec+= arr_temp[2][i];
}
int _hour = Int32.Parse(arr_temp[0])==0?0: Int32.Parse(arr_temp[0]);
int _minute = Int32.Parse(arr_temp[1]) == 0 ? 0 : Int32.Parse(arr_temp[1]);
int _sec = Int32.Parse(sec)==0?0: Int32.Parse(sec);
_hour = (time == "PM") ? _hour += 12 : _hour += 0;
_hour = (_hour < 10) ? '0' + _hour : _hour;
_minute = (_minute < 10) ? '0' + _minute : _minute;
_sec = (_sec < 10) ? '0' + _sec : _sec;
I am not getting the expected output. Please suggest.