0
Can anyone see the error with my solution?
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. 2:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock. Input Format A single string that represents a time in -hour clock format hh:mm:ss:AM or hh:mm:ss:PM Returns string: the time in 24 hour format without AM or PM MyCode: https://code.sololearn.com/cL5ubQgw8Z1v/#cs
7 Respostas
+ 4
I agree with Ani Jona 🕊's analysis and recommendation. I entered 12:34 AM (without seconds) and it printed 00:34 AM.
You can use s.IndexOf(":") to determine where to split the string, and likewise with space " ". It will help you fix the exception and the unwanted AM/PM output. Splitting on space allows it to work whether the seconds are present or not.
The output of single-digit hours needs a leading zero. An easy fix is to use a format specifier as an argument passed into ToString(). This will do it:
result=hour.ToString("D2")+minutesSeconds;
+ 4
I think input is just hour and minutes, not seconds. That will cause AM and PM to appear in the output string as mentioned by Brian.
Also, "1:35 AM" (not double digit hour), this will cause an exception.
A common way to tackle this problem, I believe, is to split the input string at ':' and then further process the components.
+ 3
The program prints AM and PM in the output, whereas the expected output should be only in hh:mm format.
+ 3
string s = Console.ReadLine();
DateTime dateTime = DateTime.Parse(s);
Console.WriteLine(dateTime);
Console.WriteLine(dateTime.ToString("T"));
Console.WriteLine (dateTime.ToString("tt"));
+ 2
With this input format: hh:mm:ss:AM or hh:mm:ss:PM, my output could be hh:mm:ss 24-hour clock, so whenjthe input was 0h:mm:ss my output return h:mm:ss instead hh:mm:ss because hour is an int type, so with hour.ToString("D2") I can resolve it... Thanks to every one.
0
Brian Wich input do you try?