一般情况下,各种语言对于DateTime?(原型Nullable<DateTime>)类型的支持不一。导致在写接口的时候,一般会考虑将这种类型用字符串来替换,在接口处理的内部会对该类型进行一个转换,最后得到日期并执行相关的业务操作。下面附上自己在开发的时候整理的正则:
- /// <summary>
- /// 是否为日期型字符串
- /// </summary>
- /// <param name="StrSource">日期字符串(2008-05-08)</param>
- /// <returns></returns>
- public static bool IsDate(string StrSource)
- {
- 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-))$");
- }
- /// <summary>
- /// 是否为时间型字符串
- /// </summary>
- /// <param name="source">时间字符串(15:00:00)</param>
- /// <returns></returns>
- public static bool IsTime(string StrSource)
- {
- return Regex.IsMatch(
- StrSource,@"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
- }
- /// <summary>
- /// 是否为日期+时间型字符串
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static bool IsDateTime_Old(string StrSource)
- {
- 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)$ ");
- }
- /// <summary>
- /// 判断用户输入是否为日期
- /// </summary>
- /// <param ></param>
- /// <returns></returns>
- /// <remarks>
- /// 可判断格式如下(其中-可替换为/,不影响验证)
- /// YYYY | YYYY-MM | YYYY-MM-DD | YYYY-MM-DD HH:MM:SS | YYYY-MM-DD HH:MM:SS.FFF
- /// </remarks>
- public static bool IsDateTime(string strValue)
- {
- if (null == strValue)
- {
- return false;
- }
- 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})?)?)?)?$";
- if (Regex.IsMatch(strValue,regexDate))
- {
- //以下各月份日期验证,保证验证的完整性
- int _IndexY = -1;
- int _IndexM = -1;
- int _IndexD = -1;
- if (-1 != (_IndexY = strValue.IndexOf("-")))
- {
- _IndexM = strValue.IndexOf("-",_IndexY + 1);
- _IndexD = strValue.IndexOf(":");
- }
- else
- {
- _IndexY = strValue.IndexOf("/");
- _IndexM = strValue.IndexOf("/",_IndexY + 1);
- _IndexD = strValue.IndexOf(":");
- }
- //不包含日期部分,直接返回true
- if (-1 == _IndexM)
- return true;
- if (-1 == _IndexD)
- {
- _IndexD = strValue.Length + 3;
- }
- int iYear = Convert.ToInt32(strValue.Substring(0,_IndexY));
- int iMonth = Convert.ToInt32(strValue.Substring(_IndexY + 1,_IndexM - _IndexY - 1));
- int iDate = Convert.ToInt32(strValue.Substring(_IndexM + 1,_IndexD - _IndexM - 4));
- //判断月份日期
- if ((iMonth < 8 && 1 == iMonth % 2) || (iMonth > 8 && 0 == iMonth % 2))
- {
- if (iDate < 32)
- return true;
- }
- else
- {
- if (iMonth != 2)
- {
- if (iDate < 31)
- return true;
- }
- else
- {
- //闰年
- if ((0 == iYear % 400) || (0 == iYear % 4 && 0 < iYear % 100))
- {
- if (iDate < 30)
- return true;
- }
- else
- {
- if (iDate < 29)
- return true;
- }
- }
- }
- }
- return false;
- }