时间格式为:HH:mm(12:39)
Parse(),ParseExact发现格式不匹配不会返回null,会产生FormatException异常。
try { DateTime TaskRunTime = DateTime.ParseExact(this.textBox_Time.Text.ToString(), "HH:mm", null); } catch (FormatException) { MessageBox.Show("请按正确的时间格式设置", "错误", MessageBoxButtons.OK,MessageBoxIcon.Error); }
将时间转化为字符串:
this.textBox_Time.Text = System.DateTime.Now.ToString("hh:mm");