isValidDateTime

Ottobre 28th, 2007

Description: Is a string a valid Datetime.

Link: http://www.codekeep.net/snippets/f10c5b90-e66b-4a25-8908-7910e2969031.aspx

  public static bool isValidDateTime(string str)
  {
    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("^([0]?[1-9]|[1][0-2])[/-]([0]?[1-9]|[1-2][0-9]|3[0-1])[/-](200[0-9])(((\x20)([0]?[1-9]|[1][0-2]):[0-5][0-9]:[0-5][0-9](\x20)?(am|AM|pm|PM))?)$");

    if (!reg.Match(str).Success)
    {
      return false;
    }

    try
    {
      DateTime dt = Convert.ToDateTime(str);
      return true;
    }
    catch
    {
      return false;
    }
  }

  • .NET
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.