如何判断字符串是否是yyyy-mm-dd格式的日期?

whizstorm 2005-06-05 11:20:05
RT
...全文
470 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ninputer 2005-06-05
  • 打赏
  • 举报
回复
有个简单的,用Like

If str Like "[1,2]###-[0,1]#-##" Then
凨叔 2005-06-05
  • 打赏
  • 举报
回复
也可以用捕捉错误的方法:

private string GetDateString(string pVal, string dateFormat)
{
string dtFLong = dateFormat;
string dtFShort = dateFormat.Replace("MM", "M").Replace("dd", "d");
string[] expectedFormats = new string[] {dtFLong, dtFLong.Replace("-", "/"), dtFLong.Replace("-", "."), dtFShort, dtFShort.Replace("-", "/"), dtFShort.Replace("-", ".")};
System.IFormatProvider format = new System.Globalization.DateTimeFormatInfo();

try
{
string strReturn = DateTime.ParseExact(pVal, expectedFormats, format, System.Globalization.DateTimeStyles.AllowWhiteSpaces).ToString("MM-dd-yyyy");
return strReturn;
}
catch(Exception exp)
{
throw(exp);
}
}
smx717616 2005-06-05
  • 打赏
  • 举报
回复
http://www.regexlib.com/Search.aspx?k=yyyy-mm-dd

16,547

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧