Time Conversion

  • + 0 comments

    C# Code

    public static string timeConversion(string s)
        {
            try
            {
                // Parse the input time into a DateTime object
                DateTime parsedTime = DateTime.ParseExact(s, "hh:mm:sstt", CultureInfo.InvariantCulture);
    
                // Convert to military (24-hour) time format
                return parsedTime.ToString("HH:mm:ss");
            }
            catch (FormatException)
            {
                throw new ArgumentException("Invalid time format. Please use hh:mm:ssAM or hh:mm:ssPM.");
            }
        }