+ 2

Military Time Problem

My Code below does not solve the puzzle, only 2 out of 5 tests passes! What am I doing wrong? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Globalization; namespace SoloLearn { class Program { static void Main(string[] args) { DateTimeFormatInfo fi = new CultureInfo( "en-US", false ).DateTimeFormat; string StrAmPm = Console.ReadLine(); DateTime myTime = DateTime.ParseExact(StrAmPm.ToUpper(), "hh:mm tt", fi); Console.WriteLine(myTime.ToString("HH:mm")); } } }

31st Dec 2019, 9:03 AM
Jan Gundersen
Jan Gundersen - avatar
3 Réponses
+ 3
Hello! I detect the problem. 1:45 AM = 01:45 ≠ 1:45 // New code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { var text = Console.ReadLine(); if (text.EndsWith(" AM")) { string result = text.Substring(0, text.Length - 3); if (result.Split(":")[0].Length == 1) { result = "0" + result; } Console.Write(result ); } else { text = text.Substring(0, text.Length - 3); string[] times = text.Split(":"); times[0] = Convert.ToString(int.Parse(times[0]) + 12); if (times[0] == "24") { times[0] = "0"; } Console.Write(times[0] + ":" + times[1]); } } } }
2nd Jan 2020, 10:46 AM
Tomáš Wróbel
Tomáš Wróbel - avatar
+ 2
I have this code (DateTime alternative) and the same problem: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { var text = Console.ReadLine(); if (text.EndsWith(" AM")) { Console.Write(text.Substring(0, text.Length - 3)); } else { text = text.Substring(0, text.Length - 3); string[] times = text.Split(":"); times[0] = Convert.ToString(int.Parse(times[0]) + 12); if (times[0] == "24") { times[0] = "0"; } Console.Write(times[0] + ":" + times[1]); } } } }
31st Dec 2019, 7:42 PM
Tomáš Wróbel
Tomáš Wróbel - avatar
+ 1
Ok! Maybe there is a problem in the tests or instructions is missing.
31st Dec 2019, 10:17 PM
Jan Gundersen
Jan Gundersen - avatar