• 时间格式验证


     #region//时间格式验证
        
    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;
        }
        
    #endregion
  • 相关阅读:
    一个长串由一个字串循环构成
    区间边界 张贴海报的可见性
    summary
    分区本质 从磁盘物理层面优化查询 分区操作的锁表
    全局变量在反汇编中是怎么体现的
    MathType怎么打定积分竖线
    定积分换元法洛必达法则求极限
    静态链表的插入和删除
    Git恢复之前版本的两种方法reset、revert
    服务去耦合
  • 原文地址:https://www.cnblogs.com/zhangwen/p/1398935.html
Copyright © 2020-2023  润新知