熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

C#正則表達式判斷輸入日期格式是否正確

2022-06-13   來源: ASP編程 

  /// <summary>

  /// 是否為日期型字符串

  /// </summary>

  /// <param name=StrSource>日期字符串()</param>

  /// <returns></returns>

  public static bool IsDate(string StrSource)

  {

  return RegexIsMatch(StrSource @^(((([]|[]\d)\d{})(?[]|[])(?[]|[]\d|[]))|((([]|[]\d)\d{})(?[]|[])(?[]|[]\d|))|((([]|[]\d)\d{})?(?[]|\d|[]))|((([]|[]\d)([]|[][]|[][])|((|[][]|[][])))?))$);

  }

  /// <summary>

  /// 是否為時間型字符串

  /// </summary>

  /// <param name=source>時間字符串(::)</param>

  /// <returns></returns>

  public static bool IsTime(string StrSource)

  {

  return RegexIsMatch(StrSource @^((||||[]?\d):[]?\d:[]?\d)$);

  }

  /// <summary>

  /// 是否為日期+時間型字符串

  /// </summary>

  /// <param name=source></param>

  /// <returns></returns>

  public static bool IsDateTime(string StrSource)

  {

  return RegexIsMatch(StrSource @^((((([]|[]\d)\d{})(?[]|[])(?[]|[]\d|[]))|((([]|[]\d)\d{})(?[]|[])(?[]|[]\d|))|((([]|[]\d)\d{})?(?[]|\d|[]))|((([]|[]\d)([]|[][]|[][])|((|[][]|[][])))?)) (||||[]?\d):[]?\d:[]?\d)$ );

  }


From:http://tw.wingwit.com/Article/program/ASP/201311/21657.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.