c# - How to determine if a string contains a specific date in any format? -


i have object property of type date , property b of type string. requirement property b may not contain string representation of property a.

right have simplistic approach of validating:

public function validate() 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 

but in approach feels wrong. date.tryparse won't work because b may have more in a.

is there approach let me validate b without typing out every possible datetime format (in variety of cultures) a?

i don't care of solution vb.net or c#.

clarification: there few format restrictions on property b. won't allow typical date delimiters of forward slash or dot or space. expect see date in mmddyyyy , ddmmyyyy or monddyyyy, etc. i'm not worried except month, day, , year.

i keep list of possible formats , iterate through it, though concern might overlook potential format way.

additional clarification property date value, not string. format not determined user - determined validation process. in following examples, b should not validate.

a = date(1962, 01, 22)  b = 01221962mynewstring or b = mystring19620122value or b = jan221962mystring etc. 

there many possibilities string representation need exclude. although suppose, don't need exclude "the22ofjanuary1962".

i use regular expressions - same issue exists. have think of every possible string representation , check it. hoping in .net framework existed , use it. sounds no such luck.

answer: marked blam's post answer. got me close, once added regular expression. iterate through possible cultures. go through relevant standard datetime formats (http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx), strip non alphanumberic characters, , compare.

not saying caught everything, unit tests pass caught ones think of today. , add custom datetime formats later if need becomes apparent.

here's solution.

dim allcultures cultureinfo()  allcultures = cultureinfo.getcultures(culturetypes.allcultures) dim rgx = new regex("[^a-za-z0-9]")  each ci 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), ""))                 return false      end if next  return true 

could enumerate cultures

culturetypes[] mostculturetypes = new culturetypes[] {             culturetypes.neutralcultures,              culturetypes.specificcultures,              culturetypes.installedwin32cultures,              culturetypes.usercustomculture,              culturetypes.replacementcultures,              }; cultureinfo[] allcultures; datetime dt = new datetime(1962, 01, 22);  // , enumerate cultures. allcultures = cultureinfo.getcultures(culturetypes.allcultures); foreach (cultureinfo ci in allcultures) {     // display name of each culture.     console.writeline("culture: {0}", ci.name);     thread.currentthread.currentculture = ci;     console.writeline("displaying short date {0} culture:",                         thread.currentthread.currentculture.name);     console.writeline("   {0} (short date string)",                         dt.toshortdatestring());     console.writeline(); } 

even appears not allowing spaces need remove spaces.
not allowing delimiters need remove them
state data format looking specific date formats.
don't want identify date formats because might miss one.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -