日期:2014-05-17  浏览次数:20974 次

正则表达式 验证日期(Asp.net)
#region 日期检测
    /// <summary>
    /// 日期验证 yyyy-MM-dd HH:mm:ss
    /// </summary>
    /// <remarks>
    /// 创建人:zhujt<br/>
    /// 创建日期:2012-08-21 10:59:25
    /// </remarks>
    /// <param name="date">验证日期</param>
    public static bool CheckDate(string date)
    {
        //date = Regex.Replace(date, @"\s", "", RegexOptions.None);  // 去除空格
        string pattern = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s((([0-1][0-9])|(2?[0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
        Regex reg = new Regex(pattern);
        return reg.IsMatch(date);
    }
    #endregion