判断日期的日期部分格式

#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 判断日期
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值