title: How to determine if a string contains a specific date in any format?
body: <p>I have an object with property A of type DATE and property B of type string.  A requirement is that property B may not contain the string representation of property A.</p>
  
  <p>right now I have the very simplistic approach of validating:</p>
  
  <pre><code>public function Validate() as boolean
      if b.contains(a.toString("[format1]", CultureInfo....)
        return false
      end if
  
      if b.contains(a.toString("[format2]", CultureInfo...)
         return false
      end if
  
      'etc
  
      return true
  
  end function
  </code></pre>
  
  <p>But something in that approach feels wrong. Date.TryParse won't work because B may have more in it than JUST A.</p>
  
  <p>Is there some approach that would let me validate B without typing out every possible datetime format (in a variety of Cultures) for A? </p>
  
  <p>I don't care of the solution is VB.net or C#.</p>
  
  <p><strong>Clarification:</strong>
  There are a few format restrictions on property B.   It won't allow the typical date delimiters of forward slash or dot or even space.  So I expect to see the date in something like mmddyyyy and ddmmyyyy or even MONddyyyy, etc.  I'm not worried about anything except for month, day, and year.</p>
  
  <p>I could just keep a list of possible formats and iterate through it, though my concern is that I might overlook a potential format that way.</p>
  
  <p><strong>Additional Clarification</strong>
  Property A is a date value, not a string.  So format is not determined by the user - it is determined by my validation process.  So in the following examples, B should NOT validate.</p>
  
  <pre><code>A = Date(1962, 01, 22)
  
  B = 01221962MyNewString
  or
  B = Mystring19620122value
  or
  B = Jan221962mystring
  etc.
  </code></pre>
  
  <p>There are many possibilities for the string representation that I would need to exclude.  Although I suppose, I don't need to exclude "the22ofjanuary1962".</p>
  
  <p>I could use regular expressions - but the same issue exists.  I simply have to think of every possible string representation and check for it.  I was hoping something in the .Net framework already existed and I could use it. But sounds like no such luck.</p>
  
  <p><strong>ANSWER:</strong>
  I marked Blam's post as the answer.  It got me really close, especially once I added a regular expression. I iterate through all possible cultures.  I then go through all the relevant standard datetime formats (<a href="http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx</a>), strip all non alphanumberic characters, and do the compare.</p>
  
  <p>Not saying it caught everything, but all my unit tests pass so it caught all the ones I could think of today.  and I could always add custom datetime formats later if the need becomes apparent.</p>
  
  <p><strong>Here's my solution.</strong></p>
  
  <pre><code>Dim allCultures As CultureInfo()
  
  allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures)
  Dim rgx = New Regex("[^a-zA-Z0-9]")
  
  For Each ci As CultureInfo In allCultures
   If B.Contains(rgx.Replace(Me.A.ToString("d", ci), "")) Or _
                  B.Contains(rgx.Replace(Me.A.ToString("D", ci), "")) Or _
                  B.Contains(rgx.Replace(Me.A.ToString("M", ci), "")) Or _
                  B.Contains(rgx.Replace(Me.A.ToString("s", ci), "")) Or _
                  B.Contains(rgx.Replace(Me.A.ToString("u", ci), "")) Or _
                  B.Contains(rgx.Replace(Me.A.ToString("Y", ci), "")) Or _
                  B.Contains(rgx.Replace(Me.A.ToString("g", ci), "")) Then
                  Return False
       End If
  Next
  
  Return True
  </code></pre>