C# 检查有效的日小时和分钟数(改进代码)

C# 检查有效的日小时和分钟数(改进代码),c#,C#,我有一个类似22:19:37格式的字符串,其中22是天,19是小时,37是分钟,这是我为验证小时和分钟而编写的 string value = "22 : 19 : 37"; string[] time = (value as string).Split(new char[] { ':' }); if (time.Length != 3) { Console.WriteLine("Error"); return; } int DD = System

我有一个类似22:19:37格式的字符串,其中22是天,19是小时,37是分钟,这是我为验证小时和分钟而编写的

string value = "22 : 19 : 37";            

string[] time = (value as string).Split(new char[] { ':' });

if (time.Length != 3)
{
     Console.WriteLine("Error");
     return;
}

int DD = System.Convert.ToInt32(time[0]);
int HH = System.Convert.ToInt32(time[1]);
int MM = System.Convert.ToInt32(time[2]);

// Hour should be less than 24
if (HH > 23)
{
    Console.WriteLine("Invalid Hour Error");
    return;
}

// Minutes should be less than 60
if (MM > 59)
{
     Console.WriteLine("Invalid Minute Error");
     return;
}

我不喜欢这段代码,是否还有改进的地方。

DateTime.TryParse
我认为这段代码速度更快,这意味着您不必使用try/catch

e、 g:


DateTime.TryParse
我相信这会更快,这意味着您不必使用try/catch

e、 g:

试试这个

string dateString = "22 : 19 : 37";
string format = "dd : HH : mm";
DateTime result;
var provider = System.Globalization.CultureInfo.InvariantCulture;

try {
   result = DateTime.ParseExact(dateString, format, provider);
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException) {
 Console.WriteLine("{0} is not in the correct format.", dateString);
}   
试试这个

string dateString = "22 : 19 : 37";
string format = "dd : HH : mm";
DateTime result;
var provider = System.Globalization.CultureInfo.InvariantCulture;

try {
   result = DateTime.ParseExact(dateString, format, provider);
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException) {
 Console.WriteLine("{0} is not in the correct format.", dateString);
}   

我同意@user3378165。但如果无法更改字符串格式,则可以使用正则表达式检查字符串是否为格式为HH:MM:SS的有效时间字符串 此函数的正则表达式:

^\d{2}:([0-1]\d|2[0-3]):([0-5]\d)$

接下来,使用
Regex
对象进行检查我同意@user3378165。但如果无法更改字符串格式,则可以使用正则表达式检查字符串是否为格式为HH:MM:SS的有效时间字符串 此函数的正则表达式:

^\d{2}:([0-1]\d|2[0-3]):([0-5]\d)$

接下来,使用
Regex
对象进行检查

,因为这似乎是一个时间跨度而不是日期时间,这里是@Nkosi的变体。也使用TryParseExact而不是ParseExact。它摆脱了丑陋的尝试捕捉

string timeSpanString = "22 : 19 : 37";
// For TimeSpan parsing all separator values need to be escaped with \\
string timeSpanFormat = "dd\\ \\:\\ hh\\ \\:\\ mm"; 
TimeSpan result;
var provider = System.Globalization.CultureInfo.InvariantCulture;

if (TimeSpan.TryParseExact(timeSpanString, timeSpanFormat, provider, out result))
{
    Console.WriteLine("{0} converts to {1}.", timeSpanString, result);
}
else
{
    Console.WriteLine("{0} is not in the correct format.", timeSpanString);
}

我认为这更清楚地表明了这个意图,@Nkosi代码将以一年一个月的方式输出数据。

由于这似乎是一个时间跨度而不是日期时间,这里是@Nkosi的变体。也使用TryParseExact而不是ParseExact。它摆脱了丑陋的尝试捕捉

string timeSpanString = "22 : 19 : 37";
// For TimeSpan parsing all separator values need to be escaped with \\
string timeSpanFormat = "dd\\ \\:\\ hh\\ \\:\\ mm"; 
TimeSpan result;
var provider = System.Globalization.CultureInfo.InvariantCulture;

if (TimeSpan.TryParseExact(timeSpanString, timeSpanFormat, provider, out result))
{
    Console.WriteLine("{0} converts to {1}.", timeSpanString, result);
}
else
{
    Console.WriteLine("{0} is not in the correct format.", timeSpanString);
}

我认为这更清楚地表明了这一意图,@Nkosi code将以一年一个月的时间输出数据。

尝试按时间跨度进行解析我认为您的意思是
改进
,而不是
即兴创作
您不需要
(值为字符串)
因为它是一个字符串已被更正@JonesopolisTry解析为时间跨度,我想你的意思是
改进
,而不是
即兴创作
你不需要
(值为字符串)
,因为它是一个字符串已被更正@Jonesopolis@vivekShukla这就是你想要的吗?我不认为这能解决问题,请注意,我有一个以天作为第一个组成部分的字符串,然后以小时和分钟作为后续内容。@vivekShukla这就是你要找的吗?我认为这不会解决问题,请注意,我有一个以天作为第一个组成部分的字符串,然后是小时和分钟。我实际上使用了TryParseExact@btlog:)我实际上使用了TryParseExact@btlog:)