5

I've got a large chunk of text like this:

....advises that Friday 22nd March 2013 has the potential to be declared a day.....

I have to locate and then parse DateTime in it (Friday 22nd March 2013). I can locate it and use DateTime.TryParse to get almost everything, but the ordinal suffixes (st,nd,rd,th) are throwing me off.

When I'm constructing the format string before calling the TryParse method are there any wildcard characters I can use to take into account those suffixes? Or any other suggestions?

Thanks!

Sundance
  • 127
  • 1
  • 8

3 Answers3

2

How about replacing the unwanted characters using a regex?

As an example the following regex should do the job: "(?<=\d)[a-z]{2}".

Example code

string date = "Friday 22nd March 2013";
var r = new Regex(@"(?<=\d)[a-z]{2}");
String result = r.Replace(date, "");
Console.WriteLine(DateTime.Parse(result, CultureInfo.InvariantCulture));

Output:

22/03/2013 00:00:00

This should take care of most cases, but make sure to test it properly.

eandersson
  • 25,781
  • 8
  • 89
  • 110
1

You could always Brute Force it.

NOTE: I'm gonna borrow some of Fuji's code so we have the same look and feel.

string date = "Friday 22nd March 2013";
string[] split = date.Split(' ');
string dayOfWeek = split[0]; // throw away & don't need
int day = Convert.ToInt32(split[1].SubString(0, split[1].Length - 2);
int month = 0;
switch (split[2]) {
  case "January": month = 1; break;
  case "February": month = 2; break;
  case "March": month = 3; break;
  case "April": month = 4; break;
  case "May": month = 5; break;
  case "June": month = 6; break;
  case "July": month = 7; break;
  case "August": month = 8; break;
  case "September": month = 9; break;
  case "October": month = 10; break;
  case "November": month = 11; break;
  case "December": month = 12; break;
}
int year = Convert.ToInt32(split[3]);
var dateTime = new DateTime(year, month, day);
Console.WriteLine(dateTime);

Not 100% I got the "new DateTime" constructor correct. I don't have my IDE running right now.

0

There is an overload of DateTime.Parse that allows multiple format strings - you can pass in the ordinals as literal strings, like this helper method (that uses the thread locale to work out month/day name spelling):

private static DateTime ParseOrdinalDateTime(string dt)
{
    string[] expectedFormats = new[] {
                                        "dddd d'st' MMMM yyyy",
                                        "dddd d'nd' MMMM yyyy",
                                        "dddd d'rd' MMMM yyyy",
                                        "dddd d'th' MMMM yyyy"
                                      };

    return DateTime.ParseExact(dt, expectedFormats, null, DateTimeStyles.None);
}
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166