您的位置:首页 > 其它

判断字符串是否符合时间格式

2008-06-28 21:26 295 查看
时间格式为: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");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: