判斷字符串是否符合時間格式

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章