時間格式爲: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");