栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP > 正则表达

C#正则表达式判断输入日期格式是否正确

正则表达 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#正则表达式判断输入日期格式是否正确

本文将介绍一段实例代码,来讲解利用正则表达式使C#判断输入日期格式是否正确的方法。希望这段代码能对大家有所帮助。

 通常我们在用C#编写系统程序或者Web开发时,都会遇到需要验证输入的字符串是否是日期的情况,下面为大家介绍一种非常全面的用正则表达式验证日期的方法: 

c 正则表达式日期代码一:

/// 
    /// 是否为日期型字符串
    /// 
    /// 日期字符串(2008-05-08)
    /// 
    public static bool IsDate(string StrSource)
    {
      return Regex.IsMatch(StrSource, @"^((((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]|1d|2[0-9]))|(((1[6-9]|[2-9]d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
    }
    /// 
    /// 是否为时间型字符串
    /// 
    /// 时间字符串(15:00:00)
    /// 
    public static bool IsTime(string StrSource)
    {
      return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?d):[0-5]?d:[0-5]?d)$");
    }
    /// 
    /// 是否为日期+时间型字符串
    /// 
    /// 
    /// 
    public static bool IsDateTime(string StrSource)
    {
      return Regex.IsMatch(StrSource, @"^(((((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]|1d|2[0-8]))|(((1[6-9]|[2-9]d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?d):[0-5]?d:[0-5]?d)$ ");
    }

c 正则表达式日期代码二:

//是否是整数
  public static bool IsInt(string StrSource)
  {
    return Regex.IsMatch(StrSource, @"^[0-9]*$");
  }

  public static bool IsDate(string strDate)
  {
    if (string.IsNullOrEmpty(strDate))
      return false;
    string s_reg = @"^(?ni:(?=\d)((?'year'((1[6-9])|([2-9]\d))\d\d)(?'sep'[/.-])(?'month'0?[1-9]|1[012])\2
(?'day'((?((0?[1-9]|1[012])(:[0-5]?\d){0,2}(\x20[AP]M))|([01]?
\d|2[0-3])(:[0-5]?\d){1,2}))?)$";
    Regex reg = new Regex(s_reg);
    if (reg.IsMatch(strDate.ToLower()))
return true;
    else
return false;
   }

以上就是小编给大家介绍的C#正则表达式判断输入日期格式是否正确的全部内容,希望对大家学习c正则表达式日期判断有所帮助。同时也非常感谢大家一直以来对考高分网网站的支持。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/60453.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号