分享几个由字符串转换为日期类型的正则

前端之家收集整理的这篇文章主要介绍了分享几个由字符串转换为日期类型的正则前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一般情况下,各种语言对于DateTime?(原型Nullable<DateTime>)类型的支持不一。导致在写接口的时候,一般会考虑将这种类型用字符串来替换,在接口处理的内部会对该类型进行一个转换,最后得到日期并执行相关的业务操作。下面附上自己在开发的时候整理的正则:

  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.  
  9.  
  10. return Regex.IsMatch(
  11. StrSource,@"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]" +
  12. @"|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|" +
  13. @"1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?" +
  14. @"2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468]" +
  15. @"[048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
  16. }
  17.  
  18. /// <summary>
  19. /// 是否为时间型字符串
  20. /// </summary>
  21. /// <param name="source">时间字符串(15:00:00)</param>
  22. /// <returns></returns>
  23. public static bool IsTime(string StrSource)
  24. {
  25. return Regex.IsMatch(
  26. StrSource,@"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
  27. }
  28.  
  29. /// <summary>
  30. /// 是否为日期+时间型字符串
  31. /// </summary>
  32. /// <param name="source"></param>
  33. /// <returns></returns>
  34. public static bool IsDateTime_Old(string StrSource)
  35. {
  36. return Regex.IsMatch(
  37. StrSource,@"^(((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?" +
  38. @"[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?" +
  39. @"[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]" +
  40. @"|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-" +
  41. @"9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[" +
  42. @"2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23" +
  43. @"|[0-1]?\d):[0-5]?\d:[0-5]?\d)$ ");
  44. }
  45.  
  46. /// <summary>
  47. /// 判断用户输入是否为日期
  48. /// </summary>
  49. /// <param ></param>
  50. /// <returns></returns>
  51. /// <remarks>
  52. /// 可判断格式如下(其中-可替换为/,不影响验证)
  53. /// YYYY | YYYY-MM | YYYY-MM-DD | YYYY-MM-DD HH:MM:SS | YYYY-MM-DD HH:MM:SS.FFF
  54. /// </remarks>
  55. public static bool IsDateTime(string strValue)
  56. {
  57. if (null == strValue)
  58. {
  59. return false;
  60. }
  61.  
  62. string regexDate = @"[1-2]{1}[0-9]{3}((-|\/){1}(([0]?[1-9]{1})|(1[0-2]{1}))((-|\/){1}((([0]?[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1})))( (([0-1]{1}[0-9]{1})|2[0-3]{1}):([0-5]{1}[0-9]{1}):([0-5]{1}[0-9]{1})(\.[0-9]{3})?)?)?)?$";
  63.  
  64. if (Regex.IsMatch(strValue,regexDate))
  65. {
  66. //以下各月份日期验证,保证验证的完整性
  67. int _IndexY = -1;
  68. int _IndexM = -1;
  69. int _IndexD = -1;
  70.  
  71. if (-1 != (_IndexY = strValue.IndexOf("-")))
  72. {
  73. _IndexM = strValue.IndexOf("-",_IndexY + 1);
  74. _IndexD = strValue.IndexOf(":");
  75. }
  76. else
  77. {
  78. _IndexY = strValue.IndexOf("/");
  79. _IndexM = strValue.IndexOf("/",_IndexY + 1);
  80. _IndexD = strValue.IndexOf(":");
  81. }
  82.  
  83. //不包含日期部分,直接返回true
  84. if (-1 == _IndexM)
  85. return true;
  86.  
  87. if (-1 == _IndexD)
  88. {
  89. _IndexD = strValue.Length + 3;
  90. }
  91.  
  92. int iYear = Convert.ToInt32(strValue.Substring(0,_IndexY));
  93. int iMonth = Convert.ToInt32(strValue.Substring(_IndexY + 1,_IndexM - _IndexY - 1));
  94. int iDate = Convert.ToInt32(strValue.Substring(_IndexM + 1,_IndexD - _IndexM - 4));
  95.  
  96. //判断月份日期
  97. if ((iMonth < 8 && 1 == iMonth % 2) || (iMonth > 8 && 0 == iMonth % 2))
  98. {
  99. if (iDate < 32)
  100. return true;
  101. }
  102. else
  103. {
  104. if (iMonth != 2)
  105. {
  106. if (iDate < 31)
  107. return true;
  108. }
  109. else
  110. {
  111. //闰年
  112. if ((0 == iYear % 400) || (0 == iYear % 4 && 0 < iYear % 100))
  113. {
  114. if (iDate < 30)
  115. return true;
  116. }
  117. else
  118. {
  119. if (iDate < 29)
  120. return true;
  121. }
  122. }
  123. }
  124. }
  125.  
  126. return false;
  127. }

猜你在找的正则表达式相关文章