天天看點

asp.net 判斷是否是日期格式,判斷是否是長日期格式,短日期格式,時間判斷,日期判斷,全部格式...

#region 判斷日期

/// <summary>
/// 判斷日期的日期部分格式
/// </summary>
/// <param name="dateStr">輸入的日期的日期部分字元串</param>
/// <returns>bool</returns>
public bool isDateTime(string dateStr)
{
    bool _isDate = false;

    string matchStr = "";

    matchStr += @"^((((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-))$ ";

    RegexOptions option = (RegexOptions.IgnoreCase | (RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace));

    if (Regex.IsMatch(dateStr, matchStr, option))
        _isDate = true;
    else
        _isDate = false;

    return _isDate;

}

/// <summary>
/// 判斷日期的時間部分格式
/// </summary>
/// <param name="time_str">輸入日期的時間部分字元串</param>
/// <returns>bool</returns>
public bool isDateTime_time(string time_str)
{ 
    bool _isDate = false;

    string matchStr = "";

    //matchStr += @"^(\s(((0?[0-9])|([1-2][0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$ ";

    //matchStr += @"(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$";

    //matchStr += @"(0*[0-9]|[1-2][0-3]):(0*[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])";

    matchStr += @"^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$";

    RegexOptions option = (RegexOptions.IgnoreCase | (RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace));

    if (Regex.IsMatch(time_str, matchStr, option))
        _isDate = true;
    else
        _isDate = false;

    return _isDate;
}

/// <summary>
/// 判斷日期的全部格式
/// </summary>
/// <param name="dateStr">輸入日期的字元串</param>
/// <returns></returns>
public bool isDate(string dateStr)
{ 
    bool _isDate = false;

    string matchStr = "";

    matchStr += @"^((((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-)) ";
    matchStr += @"(\s(((0?[0-9])|([1-2][0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$ ";

    RegexOptions option = (RegexOptions.IgnoreCase | (RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace));

    if (Regex.IsMatch(dateStr, matchStr, option))
        _isDate = true;
    else
        _isDate = false;

    return _isDate;
}

#endregion 判斷日期
           

黑色頭發:http://heisetoufa.iteye.com/

繼續閱讀