C#有沒有像VB的IsDate一樣功能的函數? DateTime.Parse 方法将日期和时间的指定字符串表示转换成其等效的 DateTime。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可是用DateTime.Parse 的時候,如果字符串不能轉換成DateTime時(如:“464654”,“2004/01/33”),程式就會出錯了,我是想判斷它是不是可以轉換成日期類型。還請指教! 用Gettype()我也試過,不過同樣的問題,用Gettype()也是需要先把字符串轉換成日期類型才能用,如果字符串不能轉換成DateTime時(如:“464654”,“2004/01/33”),程式就會出錯了。 你可以把DateTime.Parse放在try{DateTime.Parse(dt) //转换成功,肯定是日期型了}catch{//转换不成功,肯定不是日期型了.} public bool IsDateTime( string strDateTime) { bool boolreturn; try { DateTime dt = DateTime.Parse(strDateTime); boolreturn = true; } catch { bollreturn = false; } return boolreturn; } 楼上的比我快using System.Globalization; 可以到处用了 用正則表達式判別是否為正確的日期類型private bool IsValidDate(string strIn) { return Regex.IsMatch(strIn,@"^(?ni:(?=\d)((?'year'((1[6-9])|([2-9]\d))\d\d)(?'sep'[/.-])(?'month'0?[1-9]|1[012])\2(?'day'((?<!(\2((0?[2469])|11)\2))31)|(?<!\2(0?2)\2)(29|30)|((?<=((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00)\2\3\2)29)|((0?[1-9])|(1\d)|(2[0-8])))(?:(?=\x20\d)\x20|$))?((?<time>((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2}))?)$"); } 关于C#使用MSComm读串口的问题 Panel与组件的关联 Files 的值“<<<<<<< .working”无效。路径中具有非法字符。 如何根据用户的选择设置打印范围? 从outlookexpress中拖拽邮件到自己程序的问题 如何计算在一个大矩形中可以排列多少个小矩形 c#中如何将html中的table转化为xml OleDbDataReader.Read()不会自动转到下一行吗?在线等,晚上结帖 救命啊 知道图像的首地址,高度,宽度,怎么保存灰度图片 请教能将javascript写的代码页生成为.dll吗? 为什么明明是我得了分,确加不进我的总分,??
try
{
DateTime.Parse(dt) //转换成功,肯定是日期型了
}
catch
{
//转换不成功,肯定不是日期型了.
}
{
bool boolreturn;
try
{
DateTime dt = DateTime.Parse(strDateTime);
boolreturn = true;
}
catch
{
bollreturn = false;
}
return boolreturn;
}
private bool IsValidDate(string strIn)
{
return Regex.IsMatch(strIn,@"^(?ni:(?=\d)((?'year'((1[6-9])|([2-9]\d))\d\d)(?'sep'[/.-])(?'month'0?[1-9]|1[012])\2(?'day'((?<!(\2((0?[2469])|11)\2))31)|(?<!\2(0?2)\2)(29|30)|((?<=((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00)\2\3\2)29)|((0?[1-9])|(1\d)|(2[0-8])))(?:(?=\x20\d)\x20|$))?((?<time>((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2}))?)$");
}