Hi,
you could use regular expressions to extract date patterns from your text. The following code would search for a date pattern you described and return all the matches. Regular expression patern will probably require a bit more tweaking to handle all the date variations you want to handle in your application, this is just an example:
ParseDates( "I went on a walk on sunday, 12 may 2003"); ParseDates("I went on a trip on 1 August 2000 and returned on 7 May 2005");
public void ParseDates(string text) { MatchCollection dates = Regex.Matches(text,
, RegexOptions.IgnoreCase); foreach (Match date in dates) { MessageBox.Show(date.Value); } }
Of course, after getting this kind of text out of your string, you should check if it really is a date or not (using Date.TryParseExact method). Something like:
ParseDates("I went on a walk on sunday, 12 may 2003"); ParseDates("I went on a trip on 1 August 2000 and returned on 7 May 2005"); ParseDates( "I wanted to do something on 31 february 2004"); // Invalid
public void ParseDates(string text) { MatchCollection dates = Regex.Matches(text,
, RegexOptions.IgnoreCase); foreach (Match date in dates) { DateTime realDate; if (DateTime.TryParseExact(date.Value, "d MMMM yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out realDate)) { MessageBox.Show(realDate.Date.ToShortDateString()); } } }
Andrej
|