C#C
C#3y ago
Mek

✅ Converting Time???

internal string CheckTimeFormat(string input, string message)
{
    string format = @"hh\:mm\:ss";
    CultureInfo culture = CultureInfo.InvariantCulture;
    TimeSpanStyles style = TimeSpanStyles.None;
    TimeSpan result;

    try
    {
        while (!TimeSpan.TryParseExact(input, format, culture, style, out result))
        {
            Console.WriteLine("Invalid Time Input! Try Again!");
            Console.Write(message);
            input = Console.ReadLine();
        }

        return result.ToString();
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
        return null;
    }
}
How can I edit this so that I can either edit 14:45 or 02:45 PM and it still convert appropriately?
Was this page helpful?