ASP.NET 字串日期格式判斷(全部格式)

來源:互聯網
上載者:User
C#代碼
  1. #region 判斷日期   
  2.   
  3. /// <summary>   
  4. /// 判斷日期的日期部分格式   
  5. /// </summary>   
  6. /// <param name="dateStr">輸入的日期的日期部分字串</param>   
  7. /// <returns>bool</returns>   
  8. public bool isDateTime(string dateStr)   
  9. {   
  10.     bool _isDate = false;   
  11.   
  12.     string matchStr = "";   
  13.   
  14.     matchStr += @"^((((1[6-9]|[2-9]/d)/d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]/d|3[01]))|(((1[6-9]|[2-9]/d)/d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]/d|30))|(((1[6-9]|[2-9]/d)/d{2})-0?2-(0?[1-9]|1/d|2[0-8]))|(((1[6-9]|[2-9]/d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$ ";   
  15.   
  16.     RegexOptions option = (RegexOptions.IgnoreCase | (RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace));   
  17.   
  18.     if (Regex.IsMatch(dateStr, matchStr, option))   
  19.         _isDate = true;   
  20.     else  
  21.         _isDate = false;   
  22.   
  23.     return _isDate;   
  24.   
  25. }   
  26.   
  27. /// <summary>   
  28. /// 判斷日期的時間部分格式   
  29. /// </summary>   
  30. /// <param name="time_str">輸入日期的時間部分字串</param>   
  31. /// <returns>bool</returns>   
  32. public bool isDateTime_time(string time_str)   
  33. {    
  34.     bool _isDate = false;   
  35.   
  36.     string matchStr = "";   
  37.   
  38.     //matchStr += @"^(/s(((0?[0-9])|([1-2][0-3]))/:([0-5]?[0-9])((/s)|(/:([0-5]?[0-9])))))?$ ";   
  39.   
  40.     //matchStr += @"(20|21|22|23|[0-1]?/d):[0-5]?/d:[0-5]?/d$";   
  41.   
  42.     //matchStr += @"(0*[0-9]|[1-2][0-3]):(0*[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])";   
  43.   
  44.     matchStr += @"^([01]/d|2[0-3]):([0-5]/d):([0-5]/d)$";   
  45.   
  46.     RegexOptions option = (RegexOptions.IgnoreCase | (RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace));   
  47.   
  48.     if (Regex.IsMatch(time_str, matchStr, option))   
  49.         _isDate = true;   
  50.     else  
  51.         _isDate = false;   
  52.   
  53.     return _isDate;   
  54. }   
  55.   
  56. /// <summary>   
  57. /// 判斷日期的全部格式   
  58. /// </summary>   
  59. /// <param name="dateStr">輸入日期的字串</param>   
  60. /// <returns></returns>   
  61. public bool isDate(string dateStr)   
  62. {    
  63.     bool _isDate = false;   
  64.   
  65.     string matchStr = "";   
  66.   
  67.     matchStr += @"^((((1[6-9]|[2-9]/d)/d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]/d|3[01]))|(((1[6-9]|[2-9]/d)/d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]/d|30))|(((1[6-9]|[2-9]/d)/d{2})-0?2-(0?[1-9]|1/d|2[0-8]))|(((1[6-9]|[2-9]/d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) ";   
  68.     matchStr += @"(/s(((0?[0-9])|([1-2][0-3]))/:([0-5]?[0-9])((/s)|(/:([0-5]?[0-9])))))?$ ";   
  69.   
  70.     RegexOptions option = (RegexOptions.IgnoreCase | (RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace));   
  71.   
  72.     if (Regex.IsMatch(dateStr, matchStr, option))   
  73.         _isDate = true;   
  74.     else  
  75.         _isDate = false;   
  76.   
  77.     return _isDate;   
  78. }  
  79.  
  80. #endregion 判斷日期  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.