判断输入的字符是否是日期(含闰年和平年的判断)

public static bool IsDateTime(string strValue)
{
    if (strValue == 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值