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

网络编程 发布日期:2024/10/11 浏览次数:1

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

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

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

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

/// <summary>
    /// 是否为日期型字符串
    /// </summary>
    /// <param name="StrSource">日期字符串(2008-05-08)</param>
    /// <returns></returns>
    public static bool IsDate(string StrSource)
    {
      return Regex.IsMatch(StrSource, @"^((((1[6-9]|[2-9]\d)\d{2})-(0");
    }
    /// <summary>
    /// 是否为时间型字符串
    /// </summary>
    /// <param name="source">时间字符串(15:00:00)</param>
    /// <returns></returns>
    public static bool IsTime(string StrSource)
    {
      return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]");
    }
    /// <summary>
    /// 是否为日期+时间型字符串
    /// </summary>
    /// <param name="source"></param>
    /// <returns></returns>
    public static bool IsDateTime(string StrSource)
    {
      return Regex.IsMatch(StrSource, @"^(((((1[6-9]|[2-9]\d)\d{2})-(0");
    }

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 = @"^(";
    Regex reg = new Regex(s_reg);
    if (reg.IsMatch(strDate.ToLower()))
       return true;
    else
       return false;
   }

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