C
C#13mo ago
Mekasu0124

✅ 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;
}
}
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?
1 Reply
Mekasu0124
Mekasu0124OP13mo ago
internal string CheckTimeFormat(string input, string message)
{
string format = "hhmm";
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;
}
}
internal string CheckTimeFormat(string input, string message)
{
string format = "hhmm";
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;
}
}
I wound up going with this and created a helper function to display a military time conversion chart that accepts an input in the hhmm format instead to make it easier
Want results from more Discord servers?
Add your server