对于检查变量,将给出哪种条件?

时间:2015-07-08 00:50:49

标签: c#

我有两个文本框。一个用于时钟,一个用于时钟输出。时间将采用以下格式:

Hours:Minutes

我们说我已经在上午22:00钟表,并在下午2:00钟表。

我如何在C#中做到这一点。

所以,基本上,我如何为上午22:00的时间检查提供条件? 我想在这里得到错误。如果用户在22:00 AM给出了时间。

string starttime = "22:00 AM"; 
string endtime =2:00 PM;

DateTime t1 = Convert.ToDateTime(starttime); //get error:(String was not recognized as a valid DateTime.)<br>
DateTime t2 = Convert.ToDateTime(endtime); 

1 个答案:

答案 0 :(得分:2)

我从中收集到的是,当你有超出01:00到24:00或01:00 AM到12:00 PM的限制时,你想要抛出一个错误。我要做的第一件事就是决定你想用什么格式。然后,根据您正在处理的项目,如果它是winform,我将使用Masked编辑器并将其设置为您想要的格式(0:00 AM / PM或00:00)并添加errorProvider,如果它是WPF项目你必须做类似下面的代码;

<Textbox Text="{Binding Time,StringFormat=hh\\:mm}"/>

希望这会有所帮助:)

相关问题