This article is a mirror article of machine translation, please click here to jump to the original article.

View: 15421|Reply: 0

[ASP.NET] Use regular expressions to make C# determine whether the input date format is correct

[Copy link]
Posted on 6/11/2015 4:57:19 PM | | |
  1. /// <summary>  
  2.         /// 是否为日期型字符串  
  3.         /// </summary>  
  4.         /// <param name="StrSource">日期字符串(2008-05-08)</param>  
  5.         /// <returns></returns>  
  6.         public static bool IsDate(string StrSource)  
  7.         {  
  8.             return Regex.IsMatch(StrSource, @"^((((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-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");  
  9.         }  

  10.         /// <summary>  
  11.         /// 是否为时间型字符串  
  12.         /// </summary>  
  13.         /// <param name="source">时间字符串(15:00:00)</param>  
  14.         /// <returns></returns>  
  15.         public static bool IsTime(string StrSource)  
  16.         {  
  17.             return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");  
  18.         }  

  19.         /// <summary>  
  20.         /// 是否为日期+时间型字符串  
  21.         /// </summary>  
  22.         /// <param name="source"></param>  
  23.         /// <returns></returns>  
  24.         public static bool IsDateTime(string StrSource)  
  25.         {  
  26.             return Regex.IsMatch(StrSource, @"^(((((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-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$ ");  
  27.         }
Copy code






Previous:vs The C# development toolbox is empty or missing a control workaround
Next:C# is a method of determining whether a 6-digit number entered by a user is a date
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com